Skip to content

Commit 76a8bfb

Browse files
authored
Merge pull request #777 from fartem/442_Find_All_Duplicates_in_an_Array
2024-11-20 v. 7.0.7: added "442. Find All Duplicates in an Array"
2 parents 4e04499 + df2f003 commit 76a8bfb

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
572572
| 435. Non-overlapping Intervals | [Link](https://leetcode.com/problems/non-overlapping-intervals/) | [Link](./lib/medium/435_non_overlapping_intervals.rb) | [Link](./test/medium/test_435_non_overlapping_intervals.rb) |
573573
| 437. Path Sum III | [Link](https://leetcode.com/problems/path-sum-iii/) | [Link](./lib/medium/437_path_sum_iii.rb) | [Link](./test/medium/test_437_path_sum_iii.rb) |
574574
| 438. Find All Anagrams in a String | [Link](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | [Link](./lib/medium/438_find_all_anagrams_in_a_string.rb) | [Link](./test/medium/test_438_find_all_anagrams_in_a_string.rb) |
575+
| 442. Find All Duplicates in an Array | [Link](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [Link](./lib/medium/442_find_all_duplicates_in_an_array.rb) | [Link](./test/medium/test_442_find_all_duplicates_in_an_array.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.0.6'
8+
s.version = '7.0.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/find-all-duplicates-in-an-array/
4+
# @param {Integer[]} nums
5+
# @return {Integer[]}
6+
def find_duplicates(nums)
7+
nums.sort!
8+
result = []
9+
i = 0
10+
while i < nums.size
11+
if nums[i] == nums[i + 1]
12+
result << nums[i]
13+
i += 1
14+
end
15+
16+
i += 1
17+
end
18+
19+
result
20+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/442_find_all_duplicates_in_an_array'
5+
require 'minitest/autorun'
6+
7+
class FindAllDuplicatesInAnArrayTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[2, 3],
11+
find_duplicates(
12+
[4, 3, 2, 7, 8, 2, 3, 1]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
[1],
20+
find_duplicates(
21+
[1, 1, 2]
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
[],
29+
find_duplicates(
30+
[1]
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)