-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckFirstUnique.java
More file actions
45 lines (40 loc) · 1.01 KB
/
CheckFirstUnique.java
File metadata and controls
45 lines (40 loc) · 1.01 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
39
40
41
42
43
44
45
package datastructure.hashtable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* The type Check first unique.
*/
public class CheckFirstUnique {
/**
* Find first occurrence int.
*
* @param arr the arr
* @return the int
*/
public static int findFirstOccurrence(int arr[]) {
int result = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int i : arr) {
if (map.containsKey(i))
map.put(i, map.get(i) + 1);
else
map.put(i, 1);
}
for (int i : arr)
if (1 == map.get(i))
result = i;
return result;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
int[] arr = {2, 54, 7, 2, 6, 54};
System.out.println("Array: " + Arrays.toString(arr));
int unique = findFirstOccurrence(arr);
System.out.print("First Unique in an Array: " + unique);
}
}