C/C++ Program for Finding the Number Occurring Odd Number of Times?



In this article, we implement a C++ program to find the number that occurs an odd number of times in an array, using different approaches.

We are given an array containing multiple elements, and our task is to identify the number that appears an odd number of times. For example, consider the array: [1, 2, 1, 3, 3, 2, 2]. In this case, the number 2 appears 3 times, which is odd.

Example Scenarios

Let's look at a few example scenarios to understand the problem:

Input: arr[] = {5, 7, 8, 8, 5, 8, 8, 7, 7}
Output: 7
Here, the number 7 appears 3 times, which is an odd number.
Input: arr[] = {2, 3, 2, 1, 1, 4, 4}
Output: 3
In this example, the number 3 appears only once, which is also odd.

Finding Number Occurring Odd Number of Times Using Naive Method

The naive approach involves using a nested loop to count the frequency of each element. If an element's count is odd, it is returned as the result. This method is simple to implement but not optimal for large arrays, as it has a time complexity of O(n2).

Example

The following example demonstrates the naive approach to find the odd-occurring element:

#include <bits/stdc++.h>
using namespace std;

int odd_occuring_elem(int arr[], int n) {
   for (int i = 0; i < n; i++) {
      int count = 0;
      for (int j = 0; j < n; j++) {
         if (arr[i] == arr[j])
            count++;
      }
      if (count % 2 != 0)
         return arr[i];
   }
   return -1;
}

int main() {
   int arr[] = {5, 7, 8, 8, 5, 8, 8, 7, 7};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << odd_occuring_elem(arr, n);
   return 0;
}

Output

7

Time Complexity: O(n2)
Space Complexity: O(1)

Finding Number Occurring Odd Number of Times Using Bitwise XOR Operator

This approach uses the bitwise XOR operator to find the number that occurs an odd number of times. It is based on the property that the XOR of two identical numbers is 0, and the XOR of a number with 0 is the number itself. By XORing all elements in the array, the even-occurring numbers cancel out, and the remaining result is the number that appears an odd number of times.

Example

The following example demonstrates the XOR-based approach to find the odd-occurring element:

#include <bits/stdc++.h>
using namespace std;

int odd_occuring_elem(int ar[], int n) {
   int res = 0;
   for (int i = 0; i < n; i++)
      res = res ^ ar[i];
   return res;
}

int main() {
   int ar[] = {2, 3, 2, 1, 1, 4, 4};
   int n = sizeof(ar) / sizeof(ar[0]);
   cout << odd_occuring_elem(ar, n);
   return 0;
}

Output

3

Time Complexity: O(n)
Space Complexity: O(1)

Conclusion

Finding the number that occurs an odd number of times in an array can be solved using either a simple brute-force method or an efficient XOR-based approach. While the naive method is easier to understand, the XOR approach is optimal for large datasets due to its linear time and constant space complexity.

Updated on: 2025-08-01T17:48:12+05:30

407 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements