Hi I just have an array of names (strings) in flash, and I want to make sure that any duplicates from the array are removed, or at least that a function is performed only once per reocurring value in the array.
相关问题
- garbage collection best practices
- Should I wait for Flash Player 10.1 or go with Fla
- How to load flex swf from flash?
- FlashDevelop Haxe (Flash) debugger
- Converting Date with Time in PST into UTC format
相关文章
- Are there any benefits when using final in AS3?
- Trace on Chrome/Browser console
- as3 ByteArray to Hex (binary hex representation)
- getElementById not working in Google Chrome extens
- Libraries for text animation in Flex / Actionscrip
- About Collision detection alghorithms in AS3
- How to upload BitmapData to a server (ActionScript
- Manage resources to minimize garbage collection ac
This is one way to do it, I'm sure there are others.
Many ways. You can sort the array and iterate over it ignoring entries that match the previous iteration. Or you can use indexOf() to search for duplicates. Or you can take one pass over the array, build a dictionary keyed on the strings (and just ignore keys that already have an entry).
Here is the Dictionary way, memory cost of 1 boolean per unique entry, easy on memory for when you expect a lot of dupes, and fast. If you have relatively few dupes, the sort + culling of consecutive dupes is probably more efficient
Here's a sorting way, but note: THIS DOES NOT PRESERVE ORDER! You didn't say if that matters. But because it's using quicksort, it does tend to have O(N log N) performance plus the one extra pass, unless of course your data is a pathological case.
In addition to not specifying whether order matters, you did not say if it matters which of the dupes is left: the one at the lowest index, or the last one found. If that matters, you will need to re-order my dictionary example to run in the opposite direction. I started at the end because that makes it OK to do a splice without invalidating the loop count (i.e. by changing the array.length during the looping) If order matters, loop in the usual forward direction and copy out the first occurrence of each string to a new array, or modify the loop counter like this. This is probably the technique I'd use, because it preserves order and keeps the first-encountered instance of each string: