-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappearOnce.java
More file actions
36 lines (28 loc) · 927 Bytes
/
appearOnce.java
File metadata and controls
36 lines (28 loc) · 927 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
import java.util.HashMap;
public class appearOnce {
public static void main(String[] args) {
int a[] = { 1, 1, 2, 2, 3, 3, 4, 50, 50, 65, 4, 65, 5 };// find ele which one appear once
// first approach
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : a) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (int num : map.keySet()) {
if (map.get(num) == 1) {
System.out.println(" "+num);
}
}
// second approach
// for (int i = 0; i < a.length; i++) {
// int count = 0;
// for (int j = 0; j < a.length; j++) {
// if (a[i] == a[j]) {
// count++;
// }
// }
// if (count == 1){
// System.out.print(a[i]);
// }
// }
}
}