---
# System prepended metadata

title: L2-OddOccurrencesInArray
tags: [Codility_lessons]

---

# L2-OddOccurrencesInArray
###### tags: `Codility_lessons`
## Question
https://app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/
## Key
## Reference
## Solution
### Hashtable Sol.
```cpp=
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
int solution(vector<int> &A) 
{
    unordered_map <int, int> hash;

    for (unsigned int i=0; i<A.size(); i++)
    {
        hash[A[i]]++;
    }

    for (auto i : hash)
    {
        if(( i.second%2) != 0) 
        {
            return i.first;
        }
    }
    return 0;
}
```

### XOR SOL.(bitwise operation)
```cpp=
int solution(vector<int> &A) 
{
    int result = 0;
    for (unsigned int i=0; i<A.size(); i++)
    {
        result = result ^ A[i]; // using XOR to find odd occurences. even occurences cancel themselves out. e.g: B^B = 0.
    }
    return result;
}

```