Skip to content

added "2150. Find All Lonely Numbers in the Array" #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ Solutions from [LeetCode](https://leetcode.com) on Dart.

## Medium

| Name | LeetCode | Solution |
| -------------------------------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------- |
| 12. Integer to Roman | [Link](https://leetcode.com/problems/integer-to-roman/) | [Link](./lib/medium/integer_to_roman.dart) |
| 222. Count Complete Tree Nodes | [Link](https://leetcode.com/problems/count-complete-tree-nodes/) | [Link](./lib/medium/count_complete_tree_nodes.dart) |
| 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) |
| 328. Odd Even Linked List | [Link](https://leetcode.com/problems/odd-even-linked-list/) | [Link](./lib/medium/odd_even_linked_list.dart) |
| 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) |
| 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) |
| Name | LeetCode | Solution |
| ------------------------------------------ | --------------------------------------------------------------------------- | -------------------------------------------------------------- |
| 12. Integer to Roman | [Link](https://leetcode.com/problems/integer-to-roman/) | [Link](./lib/medium/integer_to_roman.dart) |
| 222. Count Complete Tree Nodes | [Link](https://leetcode.com/problems/count-complete-tree-nodes/) | [Link](./lib/medium/count_complete_tree_nodes.dart) |
| 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) |
| 328. Odd Even Linked List | [Link](https://leetcode.com/problems/odd-even-linked-list/) | [Link](./lib/medium/odd_even_linked_list.dart) |
| 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) |
| 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) |
| 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) |

## Hard

Expand Down
17 changes: 17 additions & 0 deletions lib/medium/find_all_lonely_numbers_in_the_array.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
List<int> findLonely(List<int> nums) {
var numsWithCount = {};
for (var num in nums) {
numsWithCount[num] = (numsWithCount[num] ?? 0) + 1;
}

var result = <int>[];
for (var num in nums) {
if (numsWithCount[num] == 1 && !numsWithCount.containsKey(num - 1) && !numsWithCount.containsKey(num + 1)) {
result.add(num);
}
}

return result;
}
}
28 changes: 28 additions & 0 deletions test/medium/find_all_lonely_numbers_in_the_array_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:leetcode_dart/medium/find_all_lonely_numbers_in_the_array.dart';
import 'package:test/test.dart';

void main() {
group(
'Example tests',
() {
test(
'First',
() => expect(
[10, 8],
Solution().findLonely(
[10, 6, 5, 8],
),
),
);
test(
'Second',
() => expect(
[1, 5],
Solution().findLonely(
[1, 3, 5, 3],
),
),
);
},
);
}