https://leetcode.com/problems/majority-element/
Majority Element - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
package main
func main() {
// nums := []int{3, 2, 3}
nums := []int{2, 2, 1, 1, 1, 2, 2}
majorityElement(nums)
}
// 제출 코드
func majorityElement(nums []int) int {
numsMap := make(map[int]int)
result := 0
for _, v := range nums {
numsMap[v]++
if numsMap[v] > len(nums)/2 {
result = v
break
}
}
return result
}
'Algorithm > Go' 카테고리의 다른 글
[LeetCode] Rotate String (0) | 2021.12.20 |
---|---|
[Leetcode] Single Number (0) | 2021.12.20 |
[Leetcode] Maximum Number of Words You Can Type (0) | 2021.12.12 |
[Leetcode] Final Value of Variable After Performing Operations (0) | 2021.12.12 |
[LeetCode] Consecutive Characters (Go) (0) | 2021.12.06 |