-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_25.cpp
46 lines (32 loc) · 1001 Bytes
/
question_25.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
==> Exceptionally odd <==
Question Link:- https://practice.geeksforgeeks.org/problems/find-the-odd-occurence4820/1?page=2&difficulty[]=-1&category[]=Arrays&sortBy=submissions
Given an array of N positive integers where all numbers occur even number of times except one
number which occurs odd number of times. Find the exceptional number.
Example 1:
Input:
N = 7
Arr[] = {1, 2, 3, 2, 3, 1, 3}
Output: 3
Explaination: 3 occurs three times.
Example 2:
Input:
N = 7
Arr[] = {5, 7, 2, 7, 5, 2, 5}
Output: 5
Explaination: 5 occurs three times.
*/
#include <iostream>
#include <vector>
using namespace std;
int getOddOccurrence(int arr[], int n) {
vector<int> check(100000,0);
for ( int i = 0; i < n; i++ ) check[arr[i]]++;
for ( int i = 0; i < 100000; i++ ) if ( check[i] % 2 != 0 ) return i;
return 0;
}
int main () {
int arr[] = {5, 7, 2, 7, 5, 2, 5};
cout << getOddOccurrence(arr, 7);
return 0;
}