Method 2

Algorithm

Make array element as key and the count of elements occurence as value of the key.

Alfter adding all elements of an array, scan the map's value and wherever we get a number with odd count, then that key is the answer.

Program for Method 2

#include <bits/stdc++.h>
using namespace std;
//map to store 2 int values where first one is for the array element and second one for its count
map<int,int> M;
map<int,int>::iterator it;
int main()
{
int arr[]= {1,1,2,2,3,3,4,4,4,5,5,6,6,7,7,8,8,9,9};
int size = sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<size;i++)
{
it = M.find(arr[i]);
// if the number is not present in the map then add it by making its count as 1
if(it == M.end())
{
M.insert(make_pair(arr[i],1));
}
else //if the number is present then simply increase its count by 1
{
pair<int,int> p = *it;
M.erase(it);
//p.second+1 is done for increasing the count by 1
M.insert(make_pair(p.first,p.second+1));
}
}
for(it = M.begin(); it != M.end(); it++)
{
//check for odd as if remainder is 1 then if evaluates to true
if((it->second) % 2 )
{
cout << it->first << " occurs odd number of times\n";
return 0;
}
}
return 0;
}