-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathItemAppearOnce.cpp
More file actions
49 lines (34 loc) · 813 Bytes
/
ItemAppearOnce.cpp
File metadata and controls
49 lines (34 loc) · 813 Bytes
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
47
48
// ItemAppearOnce.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define INT_SIZE 32
int getSingle(int arr[], int n)
{
int result = 0;
int x, sum;
// Iterate through every bit
for(int i=0; i< INT_SIZE; i++)
{
// Find sum of set bits at ith position in all array elements
sum = 0;
x = (1<<i);
for(int j=0;j<n;j++)
{
if(arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if(sum%3)
result |=x;
}
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("The element with single occurrence is %d ",
getSingle(arr, n));
return 0;
}