How to remove duplicates from an sorted Array?

Given a sorted array, the task is to remove the duplicate elements from the array.

Example -

Input  : arr[] = {2, 2, 2, 2, 2}
Output : arr[] = {2}
         new size = 1

Note that the array input will be sorted.

After iterating over the array elements, we will check if the previous element is not equal to the next element (because array is sorted) and if it is we will omit that element and store rest of the elements in a new array that will be outputted to the user.

Pseudo Code:-

Input the number of elements of the array.
Input the array elements.
Repeat from i = 1 to n
if (arr[i] != arr[i+1])
temp[j++] = arr[i]
temp[j++] = arr[n-1]
Repeat from i = 1 to j
arr[i] = temp[I]
 return j.