Remove Duplicate Numbers in Array

ID: 521; easy

Solution 1 (Java)

public class Solution {
    /**
     * @param nums: an array of integers
     * @return: the number of unique integers
     */
    public int deduplication(int[] nums) {
        Map<Integer, Boolean> map = new HashMap<>();
        for (int num : nums) {
            map.putIfAbsent(num, true);
        }
        int res = 0;
        for (int key : map.keySet()) {
            nums[res++] = key;
        }
        return res;
    }
}

Notes

  • Time complexity: O(n)

  • Space complexity: O(n)

Solution 2 (Java)

Notes

  • Time complexity: O(nlogn) from sorting

  • Space complexity: O(n)

Last updated

Was this helpful?