Java - 2 lines, simple solution

  • -1
    public int majorityElement(int[] nums) {
            return nums[nums.length / 2];

    "the majority element is the element that appears more than ⌊ n/2 ⌋ times."

    thinking sorted array, the majority element needs to appear more than ⌊ n/2 ⌋ times, thus it holds the middle position.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.