Skip to content
CodeKruPro

Remove duplicates from a list, keeping the order

Coding problemEasyAsked in: Cognizant

Given a list of integers, return a new list without repeats: keep the first occurrence of every value and drop the later ones, and keep the original order.

The same task often comes with text values instead of numbers; the method is the same. The largest hidden tests have 100,000 values.

Examples

Example 1
Input: nums = [3,1,3,2,1]
Output: [3,1,2]
Example 2
Input: nums = [7,7,7]
Output: [7]
Example 3
Input: nums = []
Output: []

Constraints

  • The list holds between 0 and 100,000 integers
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in