[Facebook data engineer coding interview] remove duplicates from an array/list

Data Science Interview QuestionsCategory: Data Engineer Coding[Facebook data engineer coding interview] remove duplicates from an array/list
Pythin asked 5 months ago

You have an array\list of elements that is already sorted. Consider a sample as follows.

[0, 1, 1, 1, 2, 2, …, 231, …, 4097, 4097]

You can clearly see that we have duplicates in the above array.
Your task is to write the most efficient code which can remove these duplicates.

Do not use high level utilities, libraries or functions like set(…)

Sample run:

nums = [0, 0, 1, 1, 1, 2, 3, 4, 5, 5]

# your code here

print(nums)

[0, 1, 2, 3, 4, 5]

”’


[Content Sponsor] $750 discount on any springboard courses including Data Science Career Track which comes with a Job Guarantee!

1 Answers
Arseniy answered 1 month ago

Quick solution:
def removeDuplicates(x):
    result = []
    for i in x:
        if len(result) == 0:
            result.append(i)
        elif result[-1] != i:
            result.append(i)

    return result
 
 


[Content Sponsor] $750 discount on any springboard courses including Data Science Career Track which comes with a Job Guarantee!

Your Answer

1 + 11 =

Data Science Career Bootcamp