Java passed Solution

  • -9
      Set<Integer> result = new TreeSet<Integer>();
        Hashtable<Integer, Integer> numbers = new Hashtable<Integer, Integer>();
        int n = nums.length;
        if(n <=0)
            return  Collections.emptyList();
        int m= n/3;
        for (int i = 0; i < n; i++) {
            Integer r = numbers.get(nums[i]);
            if( r==null)
            if ((r.intValue()+1) >m) {
            } else
                numbers.put(nums[i], r.intValue()+1);
        return new ArrayList(result);

    The main idea behind this algorithm is: 1 use map to store the count 2.use set to eliminate duplication elements.

  • 2

    Your solution is neat and works well. However, the question asks us to use constant space i.e. O(1) space. Using Map / Set, linear space is used which is not allowed.

Log in to reply

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