Skip to content

Commit 6cbb4c1

Browse files
authored
Solve 217 - Contains Duplicate (#7)
1 parent 531cbef commit 6cbb4c1

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ This is a monorepo containing my solutions to LeetCode problems written in vario
99
## List of Problems/Solutions
1010

1111
- 1 - Two Sum - [Notes](notes/0001_two_sum.md) | [Elixir](elixir/lib/solutions/0001_two_sum/two_sum.ex) | [Ruby](ruby/lib/solutions/0001_two_sum/two_sum.rb)
12+
- 217 - Contains Duplicate - [Notes](notes/0217_contains_duplicate.md) | [Elixir](elixir/lib/solutions/0217_contains_duplicate/contains_duplicate.ex) | [Ruby](ruby/lib/solutions/0217_contains_duplicate/contains_duplicate.rb)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule LeetCode.Solutions.ContainsDuplicate do
2+
@moduledoc false
3+
4+
@spec call([integer()]) :: boolean
5+
def call(nums) do
6+
Enum.reduce_while(nums, MapSet.new(), fn num, set ->
7+
if MapSet.member?(set, num) do
8+
{:halt, true}
9+
else
10+
{:cont, MapSet.put(set, num)}
11+
end
12+
end) == true
13+
end
14+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule LeetCode.Solutions.ContainsDuplicateTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LeetCode.Solutions.ContainsDuplicate
5+
6+
test "Case 1 works" do
7+
assert ContainsDuplicate.call([1, 2, 3, 1]) == true
8+
end
9+
10+
test "Case 2 works" do
11+
assert ContainsDuplicate.call([1, 2, 3, 4]) == false
12+
end
13+
14+
test "Case 3 works" do
15+
assert ContainsDuplicate.call([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]) == true
16+
end
17+
18+
test "Edge Case 1 works" do
19+
assert ContainsDuplicate.call([3, 3]) == true
20+
end
21+
22+
test "Edge Case 2 works" do
23+
assert ContainsDuplicate.call([0, 4, 5, 0, 3, 6]) == true
24+
end
25+
end

notes/0217_contains_duplicate.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Contains Duplicate
2+
3+
**Link to Problem**: https://leetcode.com/problems/contains-duplicate
4+
5+
## Solutions
6+
7+
- [Elixir](../elixir/lib/solutions/0217_contains_duplicate/contains_duplicate.ex)
8+
- [Ruby](../ruby/lib/solutions/0217_contains_duplicate/contains_duplicate.rb)
9+
10+
## Description
11+
12+
Given an integer array nums, return true if any value appears at least twice in the array,
13+
and return false if every element is distinct.
14+
15+
## Examples
16+
17+
### Example 1
18+
19+
```
20+
Input: nums = [1,2,3,1]
21+
Output: true
22+
```
23+
24+
### Example 2
25+
26+
```
27+
Input: nums = [1,2,3,4]
28+
Output: false
29+
```
30+
31+
### Example 3
32+
33+
```
34+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
35+
Output: true
36+
```
37+
38+
## Approach
39+
40+
The trick is to iterate through the list/array and immediately return `true` once we get a
41+
duplicate. If not, we then return `false`.
42+
43+
We can determine duplicates by using a hash map or set. We store the value to the data structure
44+
as we go along the list and hitting an existing member/attribute means that we have a duplicate.
45+
46+
## Thoughts
47+
48+
The solution for Ruby is a lot more simple because Ruby allows early `return` that will
49+
immediately halt the method.
50+
51+
For Elixir, it is possible to do the same thing through `Enum.reduce_while`, but the runtime
52+
is so different across both of them.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
def contains_duplicate(nums)
4+
hash = {}
5+
6+
nums.each do |num|
7+
return true if hash[num]
8+
9+
hash[num] = 1
10+
end
11+
12+
false
13+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require_relative '../../../lib/solutions/0217_contains_duplicate/contains_duplicate'
5+
6+
class ContainsDuplicateTest < Minitest::Test
7+
def test_case_1_works
8+
assert contains_duplicate([1, 2, 3, 1])
9+
end
10+
11+
def test_case_2_works
12+
refute contains_duplicate([1, 2, 3, 4])
13+
end
14+
15+
def test_case_3_works
16+
assert contains_duplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])
17+
end
18+
end

0 commit comments

Comments
 (0)