Majority Element LeetCode solution in Java

1
class Solution {
2
public int majorityElement(int[] nums) {
3
// Base case...
4
if (nums.length == 1) {
5
return nums[0];
6
}
7
// Sort nums array...
8
Arrays.sort(nums);
9
// Since the majority element appears more than n / 2 times...
10
// The n/2 -th element in the sorted nums must be the majority element...
11
return nums[nums.length / 2];
12
}
13
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0