Comments on: Programming Interview Questions 18: Find Even Occurring Element /2011/11/29/programming-interview-questions-18-find-even-occurring-element/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-18-find-even-occurring-element Information Retrieval and Machine Learning Mon, 23 Jan 2012 19:14:24 +0000 hourly 1 http://wordpress.org/?v=3.3 By: Ibby /2011/11/29/programming-interview-questions-18-find-even-occurring-element/#comment-1577 Ibby Mon, 23 Jan 2012 19:14:24 +0000 /?p=815#comment-1577 Aye doesn't make sense, My bad, have to go through the whole array once. Aye doesn’t make sense, My bad, have to go through the whole array once.

]]>
By: Arden /2011/11/29/programming-interview-questions-18-find-even-occurring-element/#comment-1576 Arden Mon, 23 Jan 2012 19:08:55 +0000 /?p=815#comment-1576 "if value != NULL it means the element has been added before and output it as the element occurring two times and break the loop." 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. “if value != NULL it means the element has been added before and output it as the element occurring two times and break the loop.”

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.

]]>
By: Ibby /2011/11/29/programming-interview-questions-18-find-even-occurring-element/#comment-1575 Ibby Mon, 23 Jan 2012 17:59:06 +0000 /?p=815#comment-1575 We can use a hashtable as we always do with problems that involve counting. Scan the array and count the occurrences of each number. Then perform a second pass from the hashtable and return the element with even count. 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. We can use a hashtable as we always do with problems that involve counting. Scan the array and count the occurrences of each number. Then perform a second pass from the hashtable and return the element with even count.

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.

]]>