Given an array that contains odd number of occurrences for all numbers except for a few elements which are present even number of times. Find the elements which have even occurrences in the array in O(n) time complexity and O(1) extra space.
Assume array contain elements in the range 0 to 63.
Input: [9, 12, 23, 10, 12, 12, 15, 23, 14, 12, 15] Output: 12, 23 and 15 Input: [1, 4, 7, 5, 9, 7, 3, 4, 6, 8, 3, 0, 3] Output: 4 and 7 Input: [4, 4, 10, 10, 4, 4, 4, 4, 10, 10] Output: 4 and 10
A simple method would be to traverse the array and store frequencies of its elements in a map. Later, print elements that have even count in the map. The solution takes O(n) time but requires extra space for storing frequencies. Below is an interesting method to solve this problem using bitwise operators.
This method assumes that long long integers are stored using 64 bits. The idea is to use XOR operator. We know that
1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 1 = 1 0 XOR 0 = 0
Consider below input –
1, 4, 7, 5, 9, 7, 3, 4, 6, 8, 3, 0, 3
If we left-shift 1 by value of each element of the array and take XOR of all the items, we will get below binary integer –
Each 1 in the i’th index from the right represents odd occurrence of element i. And each 0 in the i’th index from the right represents even or non-occurrence of element i in the array.
0 is present in 2nd, 4th and 7th position in above binary number. But 2 is not present in our array. So our answer is 4 and 7.
Below is the implementation of above idea
12 23 15
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find duplicates in a given array when elements are not limited to a range
- Find the missing number in a sorted array of limited range
- Find frequency of each element in a limited range array in less than O(n) time
- Two odd occurring elements in an array where all other occur even times
- Smallest element repeated exactly ‘k’ times (not limited to small range)
- Sum of all even occurring element in an array
- Find if array can be sorted by swaps limited to multiples of k
- Find the sum of all highest occurring elements in an Array
- Find all array elements occurring more than ⌊N/3⌋ times
- GCD of elements occurring Fibonacci number of times in an Array
- Sum of all minimum occurring elements in an Array
- Rearrange array such that all even-indexed elements in the Array is even
- Find Kth most occurring element in an Array
- Minimum partitions of maximum size 2 and sum limited by given value
- Find the Number Occurring Odd Number of Times
- C/C++ Program to Find the Number Occurring Odd Number of Times
- First element occurring k times in an array
- Print the most occurring character in an array of strings
- Find Kth element in an array containing odd elements first and then even elements
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray