-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlonelynum.java
More file actions
35 lines (33 loc) · 974 Bytes
/
Copy pathlonelynum.java
File metadata and controls
35 lines (33 loc) · 974 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
package arraylist;
import java.util.ArrayList;
public class lonelynum {
public static void alonenum(ArrayList<Integer> nums){
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < nums.size(); i++) {
int curr = nums.get(i);
// Check if curr appears only once
if (nums.indexOf(curr) != nums.lastIndexOf(curr)) {
continue; // skip if duplicate
}
boolean isLonely = true;
for (int j = 0; j < nums.size(); j++) {
if (i != j && (nums.get(j) == curr - 1 || nums.get(j) == curr + 1)) {
isLonely = false;
break;
}
}
if (isLonely) {
result.add(curr);
}
}
System.out.println(result);
}
public static void main(String[] args) {
ArrayList<Integer> nums=new ArrayList<>();
nums.add(1);
nums.add(3);
nums.add(5);
nums.add(3);
alonenum(nums);
}
}