Write an algorithm that takes a sorted list of n integers and remove the duplicate elements from the list and return the new length. The algorithm must run in O(n) time and O(1) space. We assume that: List elements are inetger Input list is already sorted

Respuesta :

Answer:

integer = [1, 2 , 4 , 6, 7, 9, 9]

integer.sort()

n = len(integer)

if n == 0 or n == 1:  

      print (n)  

b = 0

for i in range(0, n-1):  

     if integer[i] != integer[i+1]:  

             integer[b] = integer[i]  

             b += 1

integer[b] = integer[n-1]  

b += 1

print (b)

Explanation:

The code is written in python

integer = [1, 2 , 4 , 6, 7, 9, 9]

The list that takes the list of integers

integer.sort()

The list integer is sorted .

n = len(integer)

The length of the list of integer is gotten.

if n == 0 or n == 1:  

If the length is equal to 0 or 1

print (n)  

The the length

b = 0

We equate b to 0

for i in range(0, n-1):  

The "for loop" loops through 0 to length of the list - 1

if integer[i] != integer[i+1]:  

if the index value in the list is not equals to the next  index value in the list

integer[b] = integer[i]

The index value b is equal to index value of the looped item.

 b += 1

then add 1 to b

integer[b] = integer[n-1]  

The index value of b = index value of the length - 1.

b += 1

then add 1 to b

print(b)

print the value of b which is the length of the new list.