Cosmoteer Modding Documentation

Popular Posts

Friday, 27 January 2023

Leet Problem 169: Majority Element

 Given an array nums of size n, return the majority element.


The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.


1, sort array

2. return majority value



 class Solution {

    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length/2];
    }
}

No comments:

Post a Comment