169. Majority Element.javascript

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.

Example 1:

Input: [3,2,3]
Output: 3
Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

/**
 * @param {number[]} nums
 * @return {number}
 */
var majorityElement = function(nums) {
    nums.sort();
    // return nums[nums.length / 2];    //JavaScript中除法不会返回整数
    return nums[Math.floor(nums.length / 2)];
};  
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,505评论 0 13
  • 我想了好久好久,特别想找个融洽的时间和氛围和你说点什么,节骨眼上又不知所措了,该怎样去表述出内心那份憧...
    28_b31c阅读 419评论 0 0
  • 开心眼,字力行,号瑞草庄主(瑞草山庄庄主)。 1. 开心眼其人 开心眼?就是咱,八零后,生河南。韩德美,转过圈,乐...
    夜行独语阅读 1,353评论 4 5