public class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
Two line solution, the easiest one

class Solution { public: int majorityElement(vector<int>& nums) { sort(nums.begin(),nums.end()); int n = nums.size(); return nums[n/2]; } };
Sorting will take O(nlogn) time, but we can do it in O(n).
https://leetcode.com/discuss/84039/cmooresmajorityvotealgorithmfastestsolution
Edit: we are assuming that majority will be more that half(not exactly half).