Simple Java accepted solution using sort first.


  • -1
    C
    public class Solution {
        public List<Integer> majorityElement(int[] nums) {
            int n = nums.length/3;
            Arrays.sort(nums);
            List<Integer> l = new ArrayList<>();
            for (int i = 0; i < nums.length; i ++ ) {
                if ((i+n) < nums.length && nums[i+n]== nums[i]) {
                    if (!l.contains(nums[i])) l.add(nums[i]);
                    i = i + n;
                }
            }
            return l;
        }
    }
    

Log in to reply
 

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