-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDuplicatesFromSortedArray.js
More file actions
28 lines (22 loc) · 985 Bytes
/
removeDuplicatesFromSortedArray.js
File metadata and controls
28 lines (22 loc) · 985 Bytes
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
//WAP to remove duplicate items from a sorted array
const removeDuplicatesFromSortedArray = (array) => {
//Initialize a variable to keep track of last distinct item's index
let indexOfLastDistinct = 0;
//Itrate through the array and kepp replacing array[indexOfLastDistinct] with array[i]
//and increase the value of indexOfLastDistinct if the next item is distinct
for(let i = 0; i < array.length; i++) {
array[indexOfLastDistinct] = array[i];
if(array[i] !== array[i+1]) {
indexOfLastDistinct++;
}
}
//We are left with some extra elements
//find the no of such elements by substracting indexOfLastDistinct from length of array
let noOfItemsToRemove = array.length - indexOfLastDistinct;
//remove rest of the elements from the array
for(let i = 0; i < noOfItemsToRemove; i++) {
array.pop();
}
return array;
}
console.log(removeDuplicatesFromSortedArray([1,2,3,3,3,5,5,7]));