leetcode 169. 求众数

题目

给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。
示例 1:

输入: [3,2,3]
输出: 3

思路

  • 将数组排序
  • 则中间的元素即为众数
class Solution {
    public int majorityElement(int[] nums) {
         Arrays.sort(nums);
        return nums[nums.length / 2];

    }
}

你可能感兴趣的:(leetcode 169. 求众数)