forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindElementThatOccursOnlyOnce.java
More file actions
38 lines (32 loc) · 1.19 KB
/
FindElementThatOccursOnlyOnce.java
File metadata and controls
38 lines (32 loc) · 1.19 KB
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
package com.geeksforgeeks.array;
/**
* Find the element that appears once
* iDeserve Youtube good explanation
*/
public class FindElementThatOccursOnlyOnce {
public static void main(String[] args) {
System.out.println(getElementOccursOnlyOnce(new int[]{12, 1, 12, 3, 12, 1, 1, 2, 3, 3}, 3, 4));
}
/**
* @param arr
* @param N: The number of times other elements have been repeated
* @return
*/
public static int getElementOccursOnlyOnce(int[] arr, int N, int sizeOfBinary) {
int[] binRepresentationSum = new int[sizeOfBinary];
int[] binRepresentation = new int[sizeOfBinary];
for (int a : arr) {
binRepresentation = SubSetOfArray.getBinRepresentation(a, sizeOfBinary, binRepresentationSum);
}
// Let's do the modulo N to check which is missing
for (int i = 0; i < sizeOfBinary; i++) {
binRepresentationSum[i] = binRepresentationSum[i] % N;
}
// Return the final Result
int sum = 0;
for (int i = sizeOfBinary - 1, counter = 0; i >= 0; i--, counter++) {
sum += Math.pow(2, counter) * binRepresentationSum[i];
}
return sum;
}
}