Skip to content

Commit aaf409b

Browse files
authored
Create 2150-find-all-lonely-numbers-in-the-array.js
1 parent e2d30a3 commit aaf409b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var findLonely = function(nums) {
6+
nums.sort((a, b) => a - b)
7+
const cnt = {}
8+
for(let e of nums) {
9+
if(cnt[e] == null) cnt[e] = 0
10+
cnt[e]++
11+
}
12+
// console.log(cnt)
13+
const res = []
14+
for(let i = 0, n = nums.length; i < n; i++) {
15+
if(i === 0){
16+
if(nums[i + 1] !== nums[i] + 1 && cnt[nums[i]] === 1) {
17+
res.push(nums[i])
18+
}
19+
}
20+
else if(i === n - 1 ) {
21+
if(nums[i] !== nums[i - 1] + 1 && cnt[nums[i]] === 1) {
22+
res.push(nums[i])
23+
}
24+
}
25+
else if(cnt[nums[i]] === 1 && nums[i] !== nums[i - 1] + 1 && nums[i] !== nums[i + 1] - 1) {
26+
res.push(nums[i])
27+
}
28+
}
29+
30+
return res
31+
};

0 commit comments

Comments
 (0)