add: 220221 [golang]

This commit is contained in:
Lam Haoyin 2022-02-22 00:42:56 +08:00
parent a8bd54d0d2
commit 2ceb773065
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 18 additions and 0 deletions

18
golang/2202/220221.go Normal file
View File

@ -0,0 +1,18 @@
package main
func majorityElement(nums []int) int {
var m = make(map[int]int)
n := len(nums) >> 1
for _, v := range nums {
m[v]++
if m[v] > n {
return v
}
}
// Never reaches
return 0
}
func main() {
// Tested in C++
}