From a84bbcc30c8fbf47855574fa9e0742b238666c5f Mon Sep 17 00:00:00 2001 From: altruistcoder Date: Tue, 13 Aug 2019 23:59:45 +0530 Subject: [PATCH] Remove Duplicates from sorted array --- remove_duplicates_sorted_array.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 remove_duplicates_sorted_array.py diff --git a/remove_duplicates_sorted_array.py b/remove_duplicates_sorted_array.py new file mode 100644 index 0000000..627aa13 --- /dev/null +++ b/remove_duplicates_sorted_array.py @@ -0,0 +1,18 @@ +def removeDuplicates(a, n): + if n == 0 or n == 1: + return n + j = 0 + for i in range(0, n - 1): + if a[i] != a[i + 1]: + a[j] = a[i] + j += 1 + a[j] = a[n - 1] + j += 1 + return j + + +a = list(map(int, input("Enter a list of elements: ").split())) +n = len(a) +m = removeDuplicates(a, n) +print("The resultant array is:") +[print(a[i], end=" ") for i in range(m)]