My First Try :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import "sort"
func containsDuplicate(nums []int) bool {
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
for idx , _ := range nums {
if idx < (len(nums)-1) {
if nums[idx] == nums[idx+1] {
return true
}
}
}
return false
}
Second try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func containsDuplicate(nums []int) bool {
hashmap := make(map[int]int)
for _ , num := range nums {
hashmap[num] = 1
}
if len(nums)-len(hashmap) > 0 || len(nums)-len(hashmap) < 0 {
return true
}
return false
}