36.8k views
5 votes
1. Given a sequential list with n numbers, represented in a one-dimensional array A) Write an algorithm to check if the list has any duplication. Explain the time complexity of your algorithm. Can you think of a better algorithm to do the job? If so, briefly explain. B) Write an algorithm to find all the duplicate numbers in the list, if any. Explain the time complexity of your algorithm. Can you think of a better algorithm to do the job? If so, briefly explain.

User Maupertius
by
4.5k points

1 Answer

5 votes

Answer:

Define a function that accept a list as an argument. Save the length of the list as a variable and cast the list to a set and also save the length to a variable.

If the result of the subtraction of the set length from the list length is not equal to zero, then the list have duplicates.

Step-by-step explanation:

A sample of the source code using Python,

def list_duplicate( list ):

list_len = len( list )

new_set = set( list )

set_len = len( new_set )

If list_len - set_len != 0 :

Print( " The list has duplicate " )

User Fernandopasik
by
5.5k points