Ask Question
9 April, 13:40

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

+1
Answers (1)
  1. 9 April, 14:32
    0
    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers