Skip to main content

Command Palette

Search for a command to run...

duplicate-O(1)

Published
1 min readView as Markdown


class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> duplicates = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int value = Math.abs(nums[i]);
            int index = value - 1;
            if (nums[index] < 0) {
                duplicates.add(value);
            } else {
                nums[index] = -nums[index];
            }
        }
        return duplicates;
    }
}

More from this blog

Amit singh's blog

235 posts