-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
61 lines (51 loc) · 1.15 KB
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"sort"
)
func containDuplicateBrute(nums []int) bool {
var flag bool
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
if nums[i] == nums[j] {
return true
}
}
}
return flag
}
func containDuplicateSorting(nums []int) bool {
var flag bool
sort.Ints(nums)
for i := 0; i < len(nums)-1; i++ {
if nums[i] == nums[i+1] {
return true
}
}
return flag
}
func containDuplicateMaps(nums []int) bool {
var flag bool
mapOfArray := make(map[int]bool, len(nums))
for _, item := range nums {
if val := mapOfArray[item]; val {
return true
}
mapOfArray[item] = true
}
return flag
}
func containDuplicateSets(nums []int) bool {
mapOfArray := make(map[int]bool)
for _, item := range nums {
mapOfArray[item] = true
}
return len(nums) != len(mapOfArray)
}
func main() {
arr := []int{1, 4, 6, 7, 20, 5, 2, 11, 15, 3, 5}
fmt.Printf("Founnd duplicates 1 %v\n", containDuplicateBrute(arr))
fmt.Printf("Founnd duplicates 2 %v\n", containDuplicateSorting(arr))
fmt.Printf("Founnd duplicates 3 %v\n", containDuplicateMaps(arr))
fmt.Printf("Founnd duplicates 4 %v\n", containDuplicateSets(arr))
}