If you want to know how to do it in constant space (and linear time):
functionremoveDuplicates(arr){letnextNonDuplicate=0;for(leti=0;i<arr.length;i++){// If this is the last element or it's different from the next one we move it.if((i>=arr.length-1)||(arr[i]!=arr[i+1])){arr[nextNonDuplicate++]=arr[i];}}arr.length=nextNonDuplicate;}
You can think of nextNonDuplicate as the index of a new array, if we were using a secondary array to store the non duplicated elements.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If you want to know how to do it in constant space (and linear time):
You can think of
nextNonDuplicate
as the index of a new array, if we were using a secondary array to store the non duplicated elements.