Simple java solution using sort


  • 0
    M

    public class Solution {
    public int majorityElement(int[] num) {
    int len = num.length/2;
    if(len==0){
    return num[0];
    }
    Arrays.sort(num);
    if(num[len]==num[len-1]){
    return num[len-1];
    }else{
    return num[len];
    }

    }
    

    }


Log in to reply
 

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