Skip to content

Commit 256c5a6

Browse files
Added task 2150.
1 parent a33018a commit 256c5a6

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2101_2200.s2150_find_all_lonely_numbers_in_the_array;
2+
3+
// #Medium #Array #Hash_Table #Counting #2022_05_31_Time_93_ms_(70.66%)_Space_63.5_MB_(80.52%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public List<Integer> findLonely(int[] nums) {
11+
List<Integer> ans = new ArrayList<>();
12+
HashMap<Integer, Integer> m = new HashMap<>();
13+
for (int i : nums) {
14+
m.put(i, m.getOrDefault(i, 0) + 1);
15+
}
16+
for (int i : nums) {
17+
if (m.get(i) == 1 && !m.containsKey(i - 1) && !m.containsKey(i + 1)) {
18+
ans.add(i);
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2150\. Find All Lonely Numbers in the Array
2+
3+
Medium
4+
5+
You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
6+
7+
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [10,6,5,8]
12+
13+
**Output:** [10,8]
14+
15+
**Explanation:**
16+
17+
- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
18+
19+
- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
20+
21+
- 5 is not a lonely number since 6 appears in nums and vice versa.
22+
23+
Hence, the lonely numbers in nums are [10, 8].
24+
25+
Note that [8, 10] may also be returned.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,3,5,3]
30+
31+
**Output:** [1,5]
32+
33+
**Explanation:**
34+
35+
- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
36+
37+
- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
38+
39+
- 3 is not a lonely number since it appears twice.
40+
41+
Hence, the lonely numbers in nums are [1, 5].
42+
43+
Note that [5, 1] may also be returned.
44+
45+
**Constraints:**
46+
47+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
48+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2101_2200.s2150_find_all_lonely_numbers_in_the_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void findLonely() {
12+
assertThat(
13+
new Solution().findLonely(new int[] {10, 6, 5, 8}), equalTo(Arrays.asList(10, 8)));
14+
}
15+
16+
@Test
17+
void findLonely2() {
18+
assertThat(new Solution().findLonely(new int[] {1, 3, 5, 3}), equalTo(Arrays.asList(1, 5)));
19+
}
20+
}

0 commit comments

Comments
 (0)