remove duplicates in sorted array python

Solutions on MaxInterview for remove duplicates in sorted array python by the best coders in the world

showing results for - "remove duplicates in sorted array python"
Caterina
19 Jun 2020
1# function for removing duplicates
2def removeDuplicate(arr, n):
3    j = 0
4    
5    # traverse elements of arr
6    for i in range(0, n-1): 
7        # if ith element is not equal to (i+1)th element, then store ith value in arr[j]
8        if (arr[i] != arr[i+1]):
9            arr[j] = arr[i]
10            j = j+1
11
12    # store last value of arr in arr[j]
13    arr[j] = arr[n-1]
14    j = j+1
15    
16    # print first j elements of array arr
17    for i in range(0, j):
18        print("%d"%(arr[i]), end = " ")
19
20arr = [1, 3, 5, 5, 7, 9]
21n = len(arr)
22# calling function when number of elements in array is greater than 1
23if (n > 1):
24    removeDuplicate(arr, n)
25
Leonardo
21 Sep 2017
1j = 0
2
3// traverse elements of arr
4for i=0 to n-2
5    // if ith element is not equal to (i+1)th element of arr, then store ith value in arr[j]
6    if arr[i] != arr[i+1]
7        arr[j] = arr[i]
8        j += 1
9
10// store last value of arr in temp
11arr[j] = arr[n-1]
12j += 1
13
14// print first j elements of array arr
15for i=0 to j-1 
16    print arr[i]
17