Created
August 16, 2017 09:16
-
-
Save luojiyin1987/d976a5892f2d09b517de85506eca6c26 to your computer and use it in GitHub Desktop.
Majority Element
This file contains hidden or 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
| func majorityElement(nums []int) int { | |
| temp := make(map [int]int) | |
| result :=nums[0] | |
| if len(nums) == 1 { | |
| return result | |
| } | |
| for _, v := range nums { | |
| temp[v]++ | |
| } | |
| for k, v := range temp { | |
| if v >len(nums)/2 { | |
| result = k | |
| break | |
| } | |
| } | |
| return result | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment