Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once. Expected time complexity is O(n) and O(1) extra space.

**Examples:**

Input:arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3}

Output:2

In the given array all element appear three times except 2 which appears once.

Input:arr[] = {10, 20, 10, 30, 10, 30, 30}

Output:20

In the given array all element appear three times except 20 which appears once.

We can use sorting to do it in O(nLogn) time. We can also use hashing, it has the worst case time complexity of O(n), but requires extra space.

The idea is to use bitwise operators for a solution that is O(n) time and uses O(1) extra space. The solution is not easy like other XOR based solutions, because all elements appear odd number of times here. Run a loop for all elements in array. At the end of every iteration, maintain following two values.

ones: The bits that have appeared 1st time or 4th time or 7th time … etc.

twos: The bits that have appeared 2nd time or 5th time or 8th time … etc.

Finally, we return the value of ‘ones’

How to maintain the values of ‘ones’ and ‘twos’?

‘ones’ and ‘twos’ are initialized as 0. For every new element in array, find out the common set bits in the new element and previous value of ‘ones’. These common set bits are actually the bits that should be added to ‘twos’. So do bitwise OR of the common set bits with ‘twos’. ‘twos’ also gets some extra bits that appear third time. These extra bits are removed later.

Update ‘ones’ by doing XOR of new element with previous value of ‘ones’. There may be some bits which appear 3rd time. These extra bits are also removed later.

Both ‘ones’ and ‘twos’ contain those extra bits which appear 3rd time. Remove these extra bits by finding out common set bits in ‘ones’ and ‘twos’.

- Using Counter() function

Calculate the frequency of array using Counter function

Traverse in this Counter dictionary and check if any key has value 1

If the value of any key is 1 return the key

Below is the implementation:

`// C++ program for the above approach`

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`// function which find number`

`int`

`singlenumber(`

`int`

`a[],`

`int`

`N)`

`{`

` `

`// umap for finding frequency`

` `

`unordered_map<`

`int`

`,`

`int`

`>fmap;`

` `

` `

`// traverse the array for frequency`

` `

`for`

`(`

`int`

`i = 0; i < N;i++)`

` `

`{`

` `

`fmap[a[i]]++;`

` `

`}`

` `

` `

`// iterate over the map`

` `

`for`

`(`

`auto`

`it:fmap)`

` `

`{`

` `

` `

`// check frequency whether it is one or not.`

` `

`if`

`(it.second == 1)`

` `

`{`

` `

` `

`// return it as we got the answer`

` `

`return`

`it.first;`

` `

`}`

` `

`}`

`}`

`// Driver code`

`int`

`main()`

`{`

` `

` `

`// given array`

` `

`int`

`a[]={12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7};`

` `

` `

`// size of the array`

` `

`int`

`N=`

`sizeof`

`(a)/`

`sizeof`

`(a[0]);`

` `

` `

`// printing the returned value`

` `

`cout << singlenumber(a,N);`

` `

`return`

`0;`

`}`

**Output**

The element with single occurrence is 7

**Time Complexity:** O(n)