[Geeks for Geeks] Find element occuring once when all other are present thrice

Find element occuring once when all other are present thrice

Given an array of integers arr[] of length N, every element appears thrice except for one which occurs once.

Find that element which occurs once.

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int singleElement(int arr[] ,int N) {
vector<int> bi(32);
for(int i = 0; i < N; i++) {
for(int j = 0; j < 32; j++) {
if(arr[i] & (1<<j)) bi[j]++;
}
}
int res = 0;
for(int i = 0; i < 32; i++)
res |= (bi[i] % 3) << i;
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/21/PS/GeeksforGeeks/find-element-occuring-once-when-all-other-are-present-thrice/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.