Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5b9bfa3

Browse files
committed
Add solution #2150
1 parent bbef610 commit 5b9bfa3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,778 LeetCode solutions in JavaScript
1+
# 1,779 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1647,6 +1647,7 @@
16471647
2147|[Number of Ways to Divide a Long Corridor](./solutions/2147-number-of-ways-to-divide-a-long-corridor.js)|Hard|
16481648
2148|[Count Elements With Strictly Smaller and Greater Elements](./solutions/2148-count-elements-with-strictly-smaller-and-greater-elements.js)|Easy|
16491649
2149|[Rearrange Array Elements by Sign](./solutions/2149-rearrange-array-elements-by-sign.js)|Medium|
1650+
2150|[Find All Lonely Numbers in the Array](./solutions/2150-find-all-lonely-numbers-in-the-array.js)|Medium|
16501651
2154|[Keep Multiplying Found Values by Two](./solutions/2154-keep-multiplying-found-values-by-two.js)|Easy|
16511652
2161|[Partition Array According to Given Pivot](./solutions/2161-partition-array-according-to-given-pivot.js)|Medium|
16521653
2176|[Count Equal and Divisible Pairs in an Array](./solutions/2176-count-equal-and-divisible-pairs-in-an-array.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 2150. Find All Lonely Numbers in the Array
3+
* https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums. A number x is lonely when it appears only once, and
7+
* no adjacent numbers (i.e. x + 1 and x - 1) appear in the array.
8+
*
9+
* Return all lonely numbers in nums. You may return the answer in any order.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @return {number[]}
15+
*/
16+
var findLonely = function(nums) {
17+
const map = new Map();
18+
19+
for (const num of nums) {
20+
map.set(num, (map.get(num) || 0) + 1);
21+
}
22+
23+
const result = [];
24+
for (const [num, count] of map) {
25+
if (count === 1 && !map.has(num - 1) && !map.has(num + 1)) {
26+
result.push(num);
27+
}
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)