Remove Duplicates From Sorted Array LeetCode
TIME : O(N) [leetcode time : 7 ms]
SPACE: O(N) [Memory Usage : 40.2 MB]
import java.util.*;
import java.io.*;
import java.util.Set;
import java.util.HashSet;
class Solution {
public int removeDuplicates(int[] nums) {
int index=0;
int count=0;
int p=nums.length;
Set<Integer> aa=new TreeSet<Integer>();
int n=nums.length;
for(int i=0;i<n;i++){
if(aa.contains(nums[i])){
count++;
}
else
aa.add(nums[i]);
}
/*for(int b:aa){
System.out.println(b+" ");
}*/
Iterator<Integer> it = aa.iterator();
while(it.hasNext()){
nums[index]=it.next();
index++;
}
p=p-count;
/*for(int i=0;i<p;i++){
System.out.println(nums[i]);
}*/
return p;
}
}
Comments
Post a Comment