Simple Java solution beats 98%


  • 0
    A
    public List<Integer> findDuplicates(int[] nums) {
            int[] ocurrences = new int[nums.length+1];
    
            for (int i = 0; i < nums.length; i++) {
                ocurrences[nums[i]]++;
            }
            List<Integer> disappeared = new ArrayList<>();
    
            for (int i = 1; i < ocurrences.length; i++) {
                if (ocurrences[i] == 2) {
                    disappeared.add(i);
                }
            }
            return disappeared;
        }
    

Log in to reply
 

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