Find the Number Occurring Odd Number of Times in C++
ADVERTISEMENTS
Find the number occurring odd number of times in C++ language. In this program, you will learn how to find the number occurring odd number of times in C++.
Source Code
// Find the Number Occurring Odd Number of Times in C++
#include <bits/stdc++.h>
using namespace std;
// This function will find the odd number frequencies
int findSum(int arru[], int N) {
unordered_map<int, int> mp;
for (int i = 0; i < N; i++)
mp[arru[i]]++;
int sum = 0;
// It will loop to iterate through map
for (auto itr = mp.begin(); itr != mp.end(); itr++) {
if (itr->second % 2 != 0)
sum += (itr->first) *(itr->second);
}
return sum;
}
int main() {
// It will reserve array elements
int arru[] = {10, 20, 20, 10, 40, 10, 40, 10, 20};
// It will count size of the array
int N = sizeof(arru) / sizeof(arru[0]);
cout << "The sum of odd frequencies numbers: " << findSum(arru, N) << "\n";
return 0;
}
Output
The sum of odd frequencies numbers: 60