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中除法不會返回整數(shù)
    return nums[Math.floor(nums.length / 2)];
};  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,889評論 0 13
  • 我想了好久好久,特別想找個融洽的時間和氛圍和你說點什么,節(jié)骨眼上又不知所措了,該怎樣去表述出內(nèi)心那份憧...
    28_b31c閱讀 441評論 0 0
  • 開心眼,字力行,號瑞草莊主(瑞草山莊莊主)。 1. 開心眼其人 開心眼?就是咱,八零后,生河南。韓德美,轉(zhuǎn)過圈,樂...
    夜行獨語閱讀 1,410評論 4 5

友情鏈接更多精彩內(nèi)容