Simple Java Solution with TreeMap (7 lines)


  • 0
    A
    public int thirdMax(int[] nums) {
            TreeMap<Integer, Integer> myMap = new TreeMap<>();
            for(int i=0; i<nums.length; i++) myMap.put(nums[i], 0);
            List<Integer> list = new ArrayList<>();
            for(Map.Entry<Integer, Integer> entry : myMap.entrySet()) list.add(entry.getKey());
            if(list.size() == 1) return list.get(0);
            if(list.size() == 2) return list.get(1);
            return list.get(list.size()-3);
        }
    

Log in to reply
 

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