Javascript code in 123ms


  • 0
    K
    var majorityElement = function(nums) {
      var result;
      var majority = parseInt(nums.length/2);
      
      if(nums.length > 0){
        
        if(nums.length <=2){
          return nums[0];
        } else {
          
          nums.sort();
          
          return nums[majority];
        }
        
      } else {
        return false;
      }
    };

Log in to reply
 

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