Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Duplicate Elements Efficiently

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Find Duplicate Elements Efficiently. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given an array of integers, how would you find the duplicate elements efficiently?

Implement find_duplicates(nums) to return each value that appears at least twice, exactly once, in the order it is first identified as a duplicate. The input is a list of integers, and the output is a list of integers. For example, [4, 3, 2, 7, 8, 2, 3, 1] returns [2, 3]; [1, 1, 2, 2, 3] returns [1, 2].

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Return each duplicated value exactly once
  • Preserve the order in which values are first identified as duplicates

Function Signature

def find_duplicates(nums):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output