We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e2d30a3 commit aaf409bCopy full SHA for aaf409b
2150-find-all-lonely-numbers-in-the-array.js
@@ -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
23
24
25
+ else if(cnt[nums[i]] === 1 && nums[i] !== nums[i - 1] + 1 && nums[i] !== nums[i + 1] - 1) {
26
27
28
29
+
30
+ return res
31
+};
0 commit comments