Created
January 8, 2021 06:32
-
-
Save mumunuu/2ef9d49b128dbb0e8bba1e43443fdc77 to your computer and use it in GitHub Desktop.
algorithm(leetcode) Contains Duplicate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
fmt.Println(containsDuplicate([]int{1, 2, 3, 1})) //output true | |
fmt.Println(containsDuplicate([]int{1, 2, 3, 4})) //output false | |
fmt.Println(containsDuplicate([]int{1, 1, 1, 3, 3, 4, 3, 2, 4, 2})) //output true | |
} | |
func containsDuplicate(nums []int) bool { | |
answer := false | |
for i := 0; i < len(nums); i++ { | |
duplicateCount := indexOf(nums[i], nums) | |
if duplicateCount > 1 { | |
answer = true | |
break | |
} | |
} | |
return answer | |
} | |
func indexOf(element int, data []int) int { | |
count := 0 | |
for _, v := range data { | |
if element == v { | |
count++ | |
} | |
} | |
return count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment