-
Notifications
You must be signed in to change notification settings - Fork 0
/
session-09-19-2019-problem-26-remove-duplicates-from-sorted-array.java
45 lines (39 loc) · 1.58 KB
/
session-09-19-2019-problem-26-remove-duplicates-from-sorted-array.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
/**
* Question: 26. Remove Duplicates from Sorted Array
* Link: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
* Hoplite Session: 09/19/2019
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* Approach: Use 2 pointer approach. Trail pointer will keep track of the
* position of the next unique element and lead pointer will look for the next
* element unique element. As soon as Lead pointer finds a new unique element we
* overwrite it on the trail pointer and increment trail pointer. In the end
* position of trails pointer denotes number of unique numbers.
*
*/
class Solution {
public int removeDuplicates(int[] nums) {
// If zero or 1 number present no need to procced
if (nums.length <= 1)
return nums.length;
// Pointer initialized to 1 since 0th number will always be unique
int leadPointer = 1;
int trailPointer = 1;
for (; leadPointer < nums.length; leadPointer++) {
/**
* Keep moving leadPointer until it is more then the last unique element. Since
* input is a sorted list next number greater than current one will also be our
* next unique number
*/
if (nums[leadPointer] <= nums[trailPointer - 1])
continue;
// Overwrite new unique number in the trailPointer position
nums[trailPointer] = nums[leadPointer];
// Move trailPointer to next position for holding new unique element
trailPointer++;
}
return trailPointer;
}
}