Given a sorted array A[] of size N, delete all the duplicated elements from A[]. Modify the array such that if there are X distinct elements in it then the first X positions of the array should be filled with them in increasing order and return the number of distinct elements in the array.
Note: 
1. Don't use set or HashMap to solve the problem.
2. You must return the number of distinct elements(X) in the array, the generated output will print all the elements of the modified array from index 0 to X-1.
Example 1:
Input:
N = 5
Array = {2, 2, 2, 2, 2}
Output: {2}
Explanation: After removing all the duplicates 
only one instance of 2 will remain.
Example 2:
Input:
N = 3
Array = {1, 2, 2}
Output: {1, 2}
Your Task:  
You don't need to read input or print anything. Complete the function remove_duplicate() which takes the array A[] and its size N as input parameters and modifies it in place to delete all the duplicates. The function must return an integer X denoting the new modified size of the array. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ A[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Amortized analysis

Starter code: #User function template for Python



class Solution:	

	def remove_duplicate(self, A, N):

		# code here

Solutions:
class Solution:

	def remove_duplicate(self, A, N):
		i = 1
		temp = A[0]
		j = 1
		while j < N:
			if A[j] > temp:
				temp = A[j]
				A[i] = temp
				i += 1
			j += 1
		return i


class Solution:

	def remove_duplicate(self, A, N):
		i = 0
		for j in range(N):
			if A[j] != A[i]:
				i += 1
				A[i] = A[j]
		return i + 1


class Solution:

	def remove_duplicate(self, A, N):
		start = 0
		end = 1
		lar = A[0]
		for i in range(end, N):
			if A[i] != A[start]:
				A[start + 1] = A[i]
				start += 1
		return start + 1


class Solution:

	def remove_duplicate(self, A, N):
		d = []
		d.append(A[0])
		for i in range(1, N):
			if A[i - 1] != A[i]:
				d.append(A[i])
		A[:] = d
		return len(A)


class Solution:

	def remove_duplicate(self, a, x):
		n = 1
		for i in range(1, x):
			if a[i] != a[i - 1]:
				a[n] = a[i]
				n = n + 1
		return n


class Solution:

	def remove_duplicate(self, A, N):
		n = 1
		for i in range(1, N):
			if A[i] != A[i - 1]:
				A[n] = A[i]
				n += 1
		return n


class Solution:

	def remove_duplicate(self, A, N):
		element = A[0]
		index = 0
		for i in range(1, N):
			if A[i] == element:
				A[i] = None
			else:
				index += 1
				A[index] = A[i]
				element = A[i]
		return index + 1


class Solution:

	def remove_duplicate(self, A, N):
		flag = -1
		arr = []
		for i in A:
			if flag == -1:
				arr.append(i)
				flag += 1
			elif arr[flag] != i:
				arr.append(i)
				flag += 1
		A.clear()
		for i in arr:
			A.append(i)
		return flag + 1


class Solution:

	def remove_duplicate(self, A, N):
		postition = 1
		main = A[0]
		while postition < N:
			if main == A[postition]:
				A.pop(postition)
				N -= 1
			elif main < A[postition]:
				main = A[postition]
				postition += 1
		return N


class Solution:

	def remove_duplicate(self, A, N):
		s = list(set(A))
		for i in range(N):
			A.pop()
		for i in s:
			A.append(i)
		A.sort()
		return len(A)


class Solution:

	def remove_duplicate(self, A, N):
		l = 1
		for r in range(1, N):
			if A[r] != A[r - 1]:
				A[l] = A[r]
				l += 1
		return l


class Solution:

	def remove_duplicate(self, A, N):
		left = index = 0
		while index < N:
			if left != 0:
				if A[index] != A[left - 1]:
					A[left] = A[index]
					left += 1
			else:
				left += 1
			index += 1
		return left


class Solution:

	def remove_duplicate(self, arr, n):
		n = len(arr)
		l = 0
		r = 1
		while r < n:
			if arr[l] == arr[r]:
				r += 1
			else:
				arr[l + 1] = arr[r]
				l += 1
		return l + 1


class Solution:

	def remove_duplicate(self, A, N):
		i = 0
		while i < N:
			if i == N - 1:
				break
			elif A[i] == A[i + 1]:
				A.pop(i + 1)
				i -= 1
				N -= 1
			i += 1
		return len(A)


class Solution:

	def remove_duplicate(self, A, N):
		num = A[0]
		i = 1
		while i < len(A):
			if A[i] == num:
				num = A[i]
				del A[i]
				i -= 1
			else:
				num = A[i]
			i += 1
		return len(A)


class Solution:

	def remove_duplicate(self, arr, n):
		v = []
		(i, j) = (1, 0)
		v.append(arr[0])
		while i < n:
			if arr[i] != v[-1]:
				v.append(arr[i])
			i += 1
		arr[:] = v
		return len(arr)


class Solution:

	def remove_duplicate(self, a, n):
		b = set(a)
		b = list(b)
		a[:] = sorted(b)
		return len(a)


class Solution:

	def remove_duplicate(self, a, n):
		d = {}
		A = []
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = 1
				A.append(a[i])
		for i in range(len(A)):
			print(A[i], end=' ')
		return 0


class Solution:

	def remove_duplicate(self, A, N):
		A.append('#')
		i = 1
		while True:
			if A[i] == '#':
				return len(A) - 1
			if A[i - 1] == A[i]:
				A.pop(i)
			else:
				i += 1


class Solution:

	def remove_duplicate(self, A, N):
		(i, j) = (0, 1)
		n = len(A)
		while j < n:
			if A[i] != A[j]:
				i += 1
				A[i] = A[j]
			j += 1
		return i + 1


class Solution:

	def remove_duplicate(self, A, N):
		i = 0
		while i <= len(A) - 2:
			if A[i] == A[i + 1]:
				del A[i + 1]
				i = i - 1
			i = i + 1
		return len(A)


class Solution:

	def remove_duplicate(self, A, N):
		left = 1
		for i in range(1, len(A)):
			if A[i] != A[i - 1]:
				A[left] = A[i]
				left += 1
		return left


class Solution:

	def remove_duplicate(self, A, N):
		i = 0
		j = 1
		while j < len(A):
			if A[i] == A[j]:
				del A[j]
			elif A[i] != A[j]:
				i += 1
				j += 1
		return len(A)


class Solution:

	def remove_duplicate(self, A, N):
		i = 0
		while i < len(A):
			if i + 1 < len(A):
				if A[i] == A[i + 1]:
					A.pop(i + 1)
				else:
					i += 1
			else:
				i += 1
		return i


class Solution:

	def remove_duplicate(self, A, N):
		c = 0
		for i in range(N - 1, 0, -1):
			if A[i] == A[i - 1]:
				A[i] = -1
				c += 1
		for i in range(c):
			A.remove(-1)
		A.extend([-1 for i in range(c)])
		return N - c


class Solution:

	def remove_duplicate(self, A, N):
		if N == 0:
			return 0
		distinct = [A[0]]
		count = 1
		for i in range(1, N):
			if A[i] != A[i - 1]:
				distinct.append(A[i])
				count += 1
		for i in range(count):
			A[i] = distinct[i]
		return count


class Solution:

	def remove_duplicate(self, arr, N):
		prev = arr[0]
		k = 0
		for i in range(1, N):
			if arr[i] != prev:
				k += 1
				arr[k] = arr[i]
				prev = arr[i]
		return k + 1


class Solution:

	def remove_duplicate(self, A, N):
		s = 0
		l = 0
		for i in range(1, N):
			if A[l] != A[i]:
				s += 1
				A[s] = A[i]
			l += 1
		return s + 1


class Solution:

	def remove_duplicate(self, A, N):
		res = 1
		j = 0
		for i in range(N):
			if A[i] == A[j]:
				pass
			else:
				res += 1
				j += 1
				A[j] = A[i]
		return res


class Solution:

	def remove_duplicate(self, A, N):
		i = 1
		l = 0
		while i < N:
			if A[i] == A[l]:
				i += 1
			else:
				l += 1
				A[l] = A[i]
				i += 1
		return l + 1


from collections import Counter

class Solution:

	def remove_duplicate(self, A, N):
		county = Counter(A)
		for (i, j) in county.items():
			for k in range(j - 1):
				A.remove(i)
		return len(A)


class Solution:

	def remove_duplicate(self, A, N):
		ptr1 = 0
		ptr2 = 0
		k = 1
		while ptr2 < N:
			if A[ptr1] == A[ptr2]:
				ptr2 += 1
			else:
				A[ptr1 + 1] = A[ptr2]
				ptr1 += 1
				ptr2 += 1
				k += 1
		return k


class Solution:

	def remove_duplicate(self, A, N):
		pos = 0
		next = 1
		count = 1
		while next < N:
			if A[pos] != A[next]:
				pos += 1
				A[count] = A[next]
				count += 1
			next += 1
		return count


class Solution:

	def remove_duplicate(self, A, N):
		p1 = 0
		p2 = 1
		pos = 1
		c = 1
		while p2 < N:
			if A[p2] != A[p1]:
				A[pos] = A[p2]
				pos += 1
				p1 += 1
				c += 1
			else:
				p2 += 1
		return c


class Solution:

	def remove_duplicate(self, a, n):
		index = 0
		note = 0
		for i in range(n):
			if note != a[i]:
				a[index] = a[i]
				note = a[i]
				index += 1
		return index