可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I came across following problem:
'Find all the elements in an array which occur odd number of times'.
My thoughts about this are:
Use HashMap: Add values in the array as keys in the HashMap. The value corresponding to each key will be number of times that key is encountered.
Sort the array using Quick sort in O(N log N) and then traverse through the array to check which elements occur odd number of times.
What do you think, is there any other approach to this? If no, then which of these 2 approaches is better?
Thanks in advance!
回答1:
"Better" depends on context. Using the hash map or hash set will be faster and has the advantage of not modifying the original array, but it requires O(N) extra memory. Sorting and counting takes longer and modifies the array, but requires no extra memory.
Which solution you choose depends on whether you can afford the extra memory.
回答2:
You can modify your first approach to use a hash set instead of a hash map.
Create an initially empty hash set. Go through the elements of your array. For each element, check the hash set: if the current array element is not in the set, add it; otherwise, remove it.
When you reach the end of the array, your hash set will contain every object that occurs in your array an odd number of times.
Since accessing elements in a hash set is O(1), this algorithm has O(N) time complexity.
回答3:
Basically this can be done as an exercise in rebalancing efficiency: you go through your list of numbers, and for each number you already have in your set, you delete it again. That way your comparison set is about half the size of the possible max. Of course, that does not change O(n lg n), and you get an allocation/deallocation on each step which is likely quite more expensive.
So it might be interesting to use a mixed strategy: quicksort is pretty fast, but you basically want to coalesce two entries whenever they are equal, and at least (n-1)/2 entries are equal. Quicksort does not really accommodate the removal of elements while sorting.
So with an array-based approach (to cut down on allocation costs), I'd rather use some heap-based approach. Whenever you chance upon equal elements, you remove one. Heap sort is pretty competitive with quicksort, and being able to prune the tree in advance is going to turn out helpful.