Skip to content

Commit 9007d8c

Browse files
committed
Remove duplicate elements from sorted Array
1 parent e6e7c2e commit 9007d8c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Problem Link: https://practice.geeksforgeeks.org/problems/remove-duplicate-elements-from-sorted-array/1
3+
4+
Given a sorted array A of size N. The function remove_duplicate takes two arguments . The first argument is the sorted
5+
array A[ ] and the second argument is 'N' the size of the array and returns the size of the new converted array A[ ]
6+
with no duplicate element.
7+
8+
Input Format:
9+
The first line of input contains T denoting the no of test cases . Then T test cases follow . The first line of each
10+
test case contains an Integer N and the next line contains N space separated values of the array A[ ] .
11+
12+
Output Format:
13+
For each test case output will be the transformed array with no duplicates.
14+
15+
Your Task:
16+
Your task to complete the function remove_duplicate which removes the duplicate elements from the array .
17+
18+
Constraints:
19+
1 <= T <= 100
20+
1 <= N <= 104
21+
1 <= A[ ] <= 106
22+
23+
Example:
24+
Input (To be used only for expected output) :
25+
2
26+
5
27+
2 2 2 2 2
28+
3
29+
1 2 2
30+
Output
31+
2
32+
1 2
33+
34+
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for
35+
Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console.
36+
The task is to complete the function specified, and not to write the full code.
37+
"""
38+
#Your code goes here
39+
if __name__=='__main__':
40+
t = int(input())
41+
for i in range(t):
42+
n = int(input())
43+
arr = list(map(int, input().strip().split()))
44+
n = remove_duplicate(n, arr)
45+
for i in range(n):
46+
print (arr[i], end=" ")
47+
print()
48+
49+
''' This is a function problem.You only need to complete the function given below '''
50+
#Your function should return an integer denoting the size of the new list
51+
def remove_duplicate(n, arr):
52+
#Code here
53+
if n == 0:
54+
return 0
55+
j = 1
56+
for i in range(1,n):
57+
if arr[i] != arr[i-1]:
58+
arr[j] = arr[i]
59+
j +=1
60+
return j

0 commit comments

Comments
 (0)