Skip to the solution
EasyDSAAsked in 20246 min read

Find duplicates in array

2.1k views

The problem

Problem, as an interviewer would state it: "Implement findDuplicates(arr) — returning every element that appears MORE THAN ONCE in the array, with each duplicate value appearing exactly ONCE in the output."

Examples:

Code
findDuplicates([1,2,3,2,4,3,3]); // [2,3]

Clarifying questions expected:

  • Should each duplicate VALUE appear exactly once in the output, regardless of how many total times it repeated in the input?
  • What real, correct order should the duplicate values themselves appear in — their own first-occurrence order, or any order?
  • Does this need to also report HOW MANY times each duplicate appeared, or just identify which values are duplicated?

Code / implementation expected: Yes — real, direct proof of correct duplicate detection, confirmation an already-unique array correctly produces an empty result, and confirmation of the output's own real order.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: findDuplicates() correctly identifies repeated values, returns an empty array for unique input, and preserves first-occurrence order of the duplicates

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 10 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

Implement a Double-Ended Queue (Deque) From Scratch

Continue