# Leetcode 150 Interview ## [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150) ## Deductions - The input array is sorted in an ascending order. - The duplicate elements need to be pushed to the back of the array, preferably by swapping, because the solution has to be in place. - The ordering of the repeated elements does not matter. - Numbers can be said to be existing in adjacent colonies, colonies of 0s, followed by colonies of 1s and so on, each colony comprises of either a single number or more. ## High Level Approach - In the end we would want a divider, to the left of which we have only unique elements in an ascending order, to the right would be all the elemts in any-order. - The divider would move only to the right, in a single pass, thus solving the problem with a proposed complexity of O(n) - We can imagine a scenario where, where at any intermediate stage, our input array is divided into three parts, the first part carries all unique numbers in ascending order, the second part contains the repeated numbers and the third part contains numbers that haven't been processed yet. For 3 sections we need 2 pointers to act as dividers.

- Let's start with 2 pointers, initially all at index 0, 1. The unique pointer at 0, the left of which exist all unique numbers. 2. The seek pointer, to the left upto ptr unique lie all repeated numbers, which also points to the first element of the number colony being processed. - In order to process a colony the bounds of a colony must be known, we need to spawn a look-ahead pointer from the seek pointer that moves right to the last number of that colony, to determine the bounds of the colony.

- When processing a number colony, the first element moves to the unique section, the rest, if any move to the repeated section and then a new number colony is processed. - When every colony is processed, atleast one unique element is addded, we swap the seek pointer value with the unique pointer value. The seek pointer always points to a new un-processed number and the unique pointer always points to the first number of the repeated section.

- We have now added a value to the unique section thus we need to increment the unique pointer, the number at the seek pointer is a repeated value and hence the seek pointer must also be incremented, to the value next to the limit pointer, to the start of a new nunmber colony until, the end of the array is reached.