Skip to content

Commit 6745217

Browse files
Non-Repeating Element
Java program to find first non-repeating element.
1 parent 34a464b commit 6745217

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Non-Repeating Element

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
class GFG {
4+
5+
static void firstNonRepeating(int arr[], int n)
6+
{
7+
// Insert all array elements in hash
8+
// table
9+
Map<Integer, Integer> m = new HashMap<>();
10+
for (int i = 0; i < n; i++) {
11+
if (m.containsKey(arr[i])) {
12+
m.put(arr[i], m.get(arr[i]) + 1);
13+
}
14+
else {
15+
m.put(arr[i], 1);
16+
}
17+
}
18+
19+
// Traverse through map only and
20+
// using for-each loop for iteration over Map.entrySet()
21+
for (Map.Entry<Integer, Integer> x : m.entrySet())
22+
if (x.getValue() == 1)
23+
System.out.print(x.getKey() + " ");
24+
}
25+
26+
// Driver code
27+
public static void main(String[] args)
28+
{
29+
int arr[] = { 9, 4, 9, 6, 7, 4 };
30+
int n = arr.length;
31+
firstNonRepeating(arr, n);
32+
}
33+
}
34+

0 commit comments

Comments
 (0)