Remove Duplicates from Sorted Array- Leetcode





class Solution {
    public int removeDuplicates(int[] nums) {
       
 TreeSet<Integer> set = new TreeSet<>();


        // Add elements to the HashSet to remove duplicates
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }

        // Copy unique elements back to the array
        int k = 0;
        for (int s : set) {
            nums[k] = s;
            k++;
        }

 

        return k;  // Return the number of unique elements
}
}


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence