forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_duplicates_sorted.java
50 lines (44 loc) · 1.18 KB
/
remove_duplicates_sorted.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/727/
// MY SOLUTION
import java.util.*;
import java.lang.*;
class Solution {
public int removeDuplicates(int[] nums) {
int count = 1;
Map<Integer,Integer> hash = new HashMap();
int k=0;
for(int i=0;i<nums.length;i++)
{
if(hash.containsValue(nums[i])==false)
{
hash.put(k,nums[i]);
//System.out.println(k);
k++;
}
}
count = hash.size();
//System.out.println(count);
int arr[] =new int[count];
for(int i=0;i<count;i++)
{
nums[i] = hash.get(i);
//System.out.println(arr[i]);
}
//nums = arr;
//System.arraycopy(arr,0,nums,0,count-1);
//System.out.println(arr.length);
return count;
}
}
// ORIGINAL SOLUTION
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}