-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesFromSortedArray.java
More file actions
87 lines (83 loc) · 2.88 KB
/
RemoveDuplicatesFromSortedArray.java
File metadata and controls
87 lines (83 loc) · 2.88 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* LeetCode problem 26. Remove Duplicates from Sorted Array:
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
*/
public class Solution {
/**
* Removes all duplicate elements in the given sorted array of integers and returns the number of unique elements
* Time Complexity: O(N), where N = the size of the array of integers
* The elements are shifted "in place", by replacing the newly found duplicate element at the index equal to the
* number of unique elements counted so far. This is done in one iteration over the given list.
* <p>
* Space Complexity: O(1)
*
* @param nums the sorted array of integers
* @return the number of unique elements
*/
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int count = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[i - 1]) {
nums[count] = nums[i];
count += 1;
}
}
return count;
}
/**
* Non-optimised shifting.
* Time Complexity: O(N^2), where N = the size of nums
* Shifts all elements down and sifts the duplicate to the end of the array every time a duplicate is found. So, in
* the worst case, N * SUM(0 to N-1) steps are taken. Hence, O(N^2).
* <p>
* Space Complexity: O(1)
*/
public int removeDuplicatesWithShift(int[] nums) {
int count = nums.length;
int pos = 0;
while (pos < count - 1) {
if (nums[i] == nums[i + 1]) {
// set duplicate to null
nums[i + 1] = Integer.MAX_VALUE;
// shift elements
nums = shiftElements(nums, i + 1);
count -= 1;
} else {
pos += 1;
}
}
return count;
}
/**
* Brute-force solution using a nested for loop to check for duplicates
* Time Complexity: O(N^2), where N = the size of nums
* Space Complexity: O(1)
*/
public int removeDuplicatesBruteForce(int[] nums) {
// for every element, loop through arr, if there is duplicate, delete and shift
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (nums[i] == nums[j] && i != j) {
nums[j] = Integer.MAX_VALUE;
nums = shiftElements(nums, idx);
}
}
}
}
/**
* Shifts all the elements after the given index down
*
* @param arr the array to shift
* @param idx the index to shift from
* @return the shifted array
*/
public int[] shiftElements(int[] arr, int idx) {
for (int i = idx; i < arr.length - 1; i++) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
return arr;
}
}