How do you know that element won’t occur in the array later? Even occurrence doesn’t mean only 2 occurrences, it can be any even number. And odd occurrence doesn’t necessarily mean 1, can be any odd number.
]]>Can’t we just pass the array once?
Use a hashMap to store the elements as a key and their count as values.
Run through the array once, do a map.get on the element, if value is null put it in the map, if value != NULL it means the element has been added before and output it as the element occurring two times and break the loop.
Worst case if all elements are odd…Ɵ(N).
Best Case anywhere in O(N), the earlier we hit the even element, the earlier the loop breaks.