DDSA Solutions

Remove duplicates in array

Time: O(n)
Space: O(1)
Advertisement

Intuition

Remove duplicates from unsorted array. HashSet approach.

Algorithm

  1. 1Iterate array. Add to set if not seen. Build result from elements first seen.

Common Pitfalls

  • For sorted: two-pointer. For unsorted: hashset maintaining insertion order. O(n).
Remove duplicates in array.java
Java
// Approach: Two-pointer technique. Move unique elements forward in-place.
// Time: O(n) Space: O(1)
import java.util.*;

class Solution {
    ArrayList<Integer> removeDuplicate(int arr[]) {
        HashSet<Integer> seen = new HashSet<>();
        ArrayList<Integer> result = new ArrayList<>();
        for (int num : arr) {
            // If the element is not in the set, add it to both the set and the result list
            if (!seen.contains(num)) {
                seen.add(num);
                result.add(num);
            }
        }

        return result;
    }
}
Advertisement
Was this solution helpful?