Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.
Examples :
Input : arr = {1, 2, 3, 2, 3, 1, 3}
Output : 3
Input : arr = {5, 7, 2, 7, 5, 2, 5}
Output : 5
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
C++
// C++ program to find the element // occurring odd number of times #include <bits/stdc++.h> using namespace std; // Function to find the element // occurring odd number of times int getOddOccurrence(int arr[], int arr_size) { for (int i = 0; i < arr_size; i++) { int count = 0; for (int j = 0; j < arr_size; j++) { if (arr[i] == arr[j]) count++; } if (count % 2 != 0) return arr[i]; } return -1; } // driver code int main() { int arr[] = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 }; int n = sizeof(arr) / sizeof(arr[0]); // Function calling cout << getOddOccurrence(arr, n); return 0; } |
5
A Better Solution is to use Hashing. Use array elements as key and their counts as value. Create an empty hash table. One by one traverse the given array elements and store counts. Time complexity of this solution is O(n). But it requires extra space for hashing.
The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring element. Please note that XOR of two elements is 0 if both elements are same and XOR of a number x with 0 is x.
Below is the implementation of the above approach.
C++
// C++ program to find the element // occurring odd number of times #include <bits/stdc++.h> using namespace std; // Function to find element occurring // odd number of times int getOddOccurrence(int ar[], int ar_size) { int res = 0; for (int i = 0; i < ar_size; i++) res = res ^ ar[i]; return res; } /* Driver function to test above function */int main() { int ar[] = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 }; int n = sizeof(ar) / sizeof(ar[0]); // Function calling cout << getOddOccurrence(ar, n); return 0; } |
5
C
// C program to find the element // occurring odd number of times #include <stdio.h> // Function to find element occurring // odd number of times int getOddOccurrence(int ar[], int ar_size) { int res = 0; for (int i = 0; i < ar_size; i++) res = res ^ ar[i]; return res; } /* Driver function to test above function */int main() { int ar[] = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 }; int n = sizeof(ar) / sizeof(ar[0]); // Function calling printf("%d", getOddOccurrence(ar, n)); return 0; } |
5
Please refer complete article on Find the Number Occurring Odd Number of Times for more details!
Recommended Posts:
- Create Directory or Folder with C/C++ Program
- C Program to Swap two Numbers
- C Program for Bubble Sort on Linked List
- C program to Replace a word in a text by another given word
- C program to invert (making negative) an image content in PGM format
- C Program for Selection Sort
- C Program for Bubble Sort
- C Program for Insertion Sort
- C Program for Radix Sort
- C/C++ Program for n-th Fibonacci number
- C Program for Counting Sort
- C Program for Binary Search (Recursive and Iterative)
- C Program for Min Cost Path
- C/C++ Program for Longest Increasing Subsequence
- C Program for Pancake sorting
- C Program for Binary Insertion Sort
- C Program for Merge Sort
- C Program for Recursive Insertion Sort
- C Program for Iterative Merge Sort
- C Program for Iterative Quick Sort

