forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNon-Repeating Element
More file actions
34 lines (29 loc) · 906 Bytes
/
Non-Repeating Element
File metadata and controls
34 lines (29 loc) · 906 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
import java.util.*;
class GFG {
static void firstNonRepeating(int arr[], int n)
{
// Insert all array elements in hash
// table
Map<Integer, Integer> m = new HashMap<>();
for (int i = 0; i < n; i++) {
if (m.containsKey(arr[i])) {
m.put(arr[i], m.get(arr[i]) + 1);
}
else {
m.put(arr[i], 1);
}
}
// Traverse through map only and
// using for-each loop for iteration over Map.entrySet()
for (Map.Entry<Integer, Integer> x : m.entrySet())
if (x.getValue() == 1)
System.out.print(x.getKey() + " ");
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 9, 4, 9, 6, 7, 4 };
int n = arr.length;
firstNonRepeating(arr, n);
}
}