[ 算法 ] Majority Element
2016-09-20 13:05
204 查看
Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always exist in the array.
/** * @param {number[]} nums * @return {number} */ var majorityElement = function(nums) { var ret = []; var max = -1; var key = -1; for(var i = 0 ; i< nums.length; i ++){ if(ret[nums[i]]){ ret[nums[i]] = ret[nums[i]] + 1; }else{ ret[nums[i]] = 1; } if(ret[nums[i]] > max){ max = ret[nums[i]]; key = nums[i]; } } if(max - (nums.length - max )){ return key; }else{ return -1; } };
相关文章推荐
- 算法课第二周作业 | Majority Element
- 算法第二周Majority Element[easy]
- LeetCode--Majority Element II & 多数投票算法
- 15算法课程 169. Majority Element
- 算法系列(13) Leetcode 169 Majority Element
- 字符串算法——查找数组多数元素(Majority Element II)
- leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)
- 【LeetCode】169. Majority Element (多数投票算法 & 算法迁移能力)
- LeetCode--Majority Element II & 多数投票算法
- 【算法分析与设计】【第五周】169. Majority Element
- (算法分析Week1)Majority Element[Easy]
- LeetCode:Majority Element I II 投票算法
- LeetCode 169. Majority Element (数组的主要元素、摩尔投票算法)
- 【leetcode】 Majority_Element
- 169. Majority Element
- LeetCode 169. Majority Element
- Majority Element
- Leetcode #169 Majority Element
- leetcode 169---Majority Element
- [LeetCode]--169. Majority Element