My java Solution 7ms


  • 0
    L

    Here is my solution, and i got 7ms running time

        public int majorityElement(int[] nums) {
            Arrays.sort(nums);
            int current = 0;
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] != nums[i-1]) {
                    current = i;
                }
                if (i - current + 1 > nums.length/2) {
                    break;
                }
            }
            return nums[current];
        }
    }```

Log in to reply
 

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