[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 3 years 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]

”’

1 Answers
Arseniy answered 3 years 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
 
 

Your Answer

18 + 3 =