Skip to content

Removing Duplicates from an Unsorted Array Using HashSet in Java #2893

Open
@ManishCody

Description

@ManishCody

public int removeDuplicates(int[] nums) {
HashSet seen = new HashSet<>();
int k = 0;

    for (int num : nums) {
        if (!seen.contains(num)) {
            seen.add(num);
            nums[k] = num;
            k++;
        }
    }
    
    return k; // The length of the unique elements
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions