Skip to content

Commit 4a90f37

Browse files
authored
Merge pull request 6boris#54 from hiepndd/develop
Add solution problem 0217
2 parents 73119a0 + e973cb5 commit 4a90f37

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [217. Contains Duplicate][title]
2+
3+
## Description
4+
5+
Given an array of integers, find if the array contains any duplicates.
6+
7+
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: [1,2,3,1]
13+
Output: true
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: [1,2,3,4]
20+
Output: false
21+
```
22+
23+
**Example 3:**
24+
25+
```
26+
Input: [1,1,1,3,3,4,3,2,4,2]
27+
Output: true
28+
```
29+
30+
[title]: https://leetcode.com/problems/contains-duplicate/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Solution
2+
3+
func containsDuplicate(nums []int) bool {
4+
disct := make(map[int]bool, len(nums))
5+
6+
for _, number := range nums {
7+
if disct[number] {
8+
return true
9+
} else {
10+
disct[number] = true
11+
}
12+
}
13+
return false
14+
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestConTainsDuplicate(t *testing.T) {
9+
cases := []struct {
10+
name string
11+
inputs []int
12+
expects bool
13+
}{
14+
{"Test case 1", []int{1, 2, 3, 1}, true},
15+
{"Test case 2", []int{1, 2, 3, 4}, false},
16+
{"Test case 3", []int{1, 1, 1, 3, 3, 4, 3, 2, 4, 2}, true},
17+
}
18+
19+
for _, testcase := range cases {
20+
t.Run(testcase.name, func(t *testing.T) {
21+
result := containsDuplicate(testcase.inputs)
22+
if !reflect.DeepEqual(result, testcase.expects) {
23+
t.Fatalf("expected: %v, but got: %v, with input: %v ", testcase.expects, result, testcase.inputs)
24+
}
25+
26+
})
27+
}
28+
}

0 commit comments

Comments
 (0)