Skip to content

Commit 9bcbff7

Browse files
Merge pull request #35 from fluttermiddlepodcast/2150-Find-All-Lonely-Numbers-in-the-Array
added "2150. Find All Lonely Numbers in the Array"
2 parents 8e98b4c + e9fb296 commit 9bcbff7

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ Solutions from [LeetCode](https://leetcode.com) on Dart.
3232

3333
## Medium
3434

35-
| Name | LeetCode | Solution |
36-
| -------------------------------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------- |
37-
| 12. Integer to Roman | [Link](https://leetcode.com/problems/integer-to-roman/) | [Link](./lib/medium/integer_to_roman.dart) |
38-
| 222. Count Complete Tree Nodes | [Link](https://leetcode.com/problems/count-complete-tree-nodes/) | [Link](./lib/medium/count_complete_tree_nodes.dart) |
39-
| 230. Kth Smallest Element in a BST | [Link](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [Link](./lib/medium/kth_smallest_element_in_a_bst.dart) |
40-
| 328. Odd Even Linked List | [Link](https://leetcode.com/problems/odd-even-linked-list/) | [Link](./lib/medium/odd_even_linked_list.dart) |
41-
| 1325. Delete Leaves With a Given Value | [Link](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Link](./lib/medium/delete_leaves_with_a_given_value.dart) |
42-
| 1670. Design Front Middle Back Queue | [Link](https://leetcode.com/problems/design-front-middle-back-queue/) | [Link](./lib/medium/design_front_middle_back_queue.dart) |
35+
| Name | LeetCode | Solution |
36+
| ------------------------------------------ | --------------------------------------------------------------------------- | -------------------------------------------------------------- |
37+
| 12. Integer to Roman | [Link](https://leetcode.com/problems/integer-to-roman/) | [Link](./lib/medium/integer_to_roman.dart) |
38+
| 222. Count Complete Tree Nodes | [Link](https://leetcode.com/problems/count-complete-tree-nodes/) | [Link](./lib/medium/count_complete_tree_nodes.dart) |
39+
| 230. Kth Smallest Element in a BST | [Link](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [Link](./lib/medium/kth_smallest_element_in_a_bst.dart) |
40+
| 328. Odd Even Linked List | [Link](https://leetcode.com/problems/odd-even-linked-list/) | [Link](./lib/medium/odd_even_linked_list.dart) |
41+
| 1325. Delete Leaves With a Given Value | [Link](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Link](./lib/medium/delete_leaves_with_a_given_value.dart) |
42+
| 1670. Design Front Middle Back Queue | [Link](https://leetcode.com/problems/design-front-middle-back-queue/) | [Link](./lib/medium/design_front_middle_back_queue.dart) |
43+
| 2150. Find All Lonely Numbers in the Array | [Link](https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/) | [Link](./lib/medium/find_all_lonely_numbers_in_the_array.dart) |
4344

4445
## Hard
4546

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
List<int> findLonely(List<int> nums) {
3+
var numsWithCount = {};
4+
for (var num in nums) {
5+
numsWithCount[num] = (numsWithCount[num] ?? 0) + 1;
6+
}
7+
8+
var result = <int>[];
9+
for (var num in nums) {
10+
if (numsWithCount[num] == 1 && !numsWithCount.containsKey(num - 1) && !numsWithCount.containsKey(num + 1)) {
11+
result.add(num);
12+
}
13+
}
14+
15+
return result;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:leetcode_dart/medium/find_all_lonely_numbers_in_the_array.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group(
6+
'Example tests',
7+
() {
8+
test(
9+
'First',
10+
() => expect(
11+
[10, 8],
12+
Solution().findLonely(
13+
[10, 6, 5, 8],
14+
),
15+
),
16+
);
17+
test(
18+
'Second',
19+
() => expect(
20+
[1, 5],
21+
Solution().findLonely(
22+
[1, 3, 5, 3],
23+
),
24+
),
25+
);
26+
},
27+
);
28+
}

0 commit comments

Comments
 (0)