# Coding Challenge - #3: Find Duplicate Values

**URL:** https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967
**Category:** web dev
**Created:** [August 6, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967 "2026-08-06T22:00:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)
#### Post date: [August 6, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967/1 "2026-08-06T22:00:09Z")

</div>

Write a function findDuplicates(arr) that returns an array of values that appear more than once in the input array, each value listed only once, in the order they first repeat. For example, findDuplicates([1,2,3,2,4,1]) should return [2,1].

```js
function findDuplicates(arr) {
  // your code here
}

```

**Rules:**

- Plain JavaScript only, no libraries
- Return values in order of first repeat, not first appearance
- Each duplicate value should appear only once in the result
- Solution should be about 3 to 10 lines

Post your solution as a reply. Answer goes up in about a day.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [August 6, 2026, 11:04pm UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967/2 "2026-08-06T23:04:27Z")

</div>

Here is my attempt:

```auto
function findDuplicates(arr) {
   let unique = new Set();
   let duplicates = new Set();
   for (let i = 0; i < arr.length; i++) {
         let current = arr[i];
         if (!unique.has(current)) {
              unique.add(current);
         } else {
              duplicates.add(current);
         }
   }
   return duplicates;
}

console.log(findDuplicates([1,2,3,2,4,1]));

```

---

<div class="post-metadata">

### Author: ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)
#### Post date: [August 6, 2026, 11:20pm UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967/3 "2026-08-06T23:20:13Z")

</div>

Logged it, no verdict from me though. Answer drops later today, try not to stare at that return type too hard until then.

---

<div class="post-metadata">

### Author: ![kirupaBot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupabot/32/31834_2.png) [@kirupaBot](https://forum.kirupa.com/u/kirupaBot)
#### Post date: [August 6, 2026, 11:20pm UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967/4 "2026-08-06T23:20:28Z")

</div>

Your logic is solid-you’re tracking unique and duplicate values correctly.

The challenge asks for an array though, not a Set.

To go deeper into this topic including some of the technical concepts called out earlier, these resources may help.

- [kirupa.com - Removing Duplicate Items from an Array](https://www.kirupa.com/html5/removing_duplicate_items_from_an_array.htm)

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [August 7, 2026, 12:50am UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967/5 "2026-08-07T00:50:24Z")

</div>

Ok - now it returns an array!

```auto
function findDuplicates(arr) {
   let unique = new Set();
   let duplicates = new Set();
   
   for (let i = 0; i < arr.length; i++) {
         let current = arr[i];
         if (!unique.has(current)) {
              unique.add(current);
         } else {
              duplicates.add(current);
         }
   }
   
   // Convert the Set to an Array before returning
   return [...duplicates];
}

console.log(findDuplicates([1, 2, 3, 2, 4, 1]));

```

---

<div class="post-metadata">

### Author: ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)
#### Post date: [August 7, 2026, 2:40am UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967/6 "2026-08-07T02:40:13Z")

</div>

sets and spread, love it, tucked that guess away for later. answer drops later today so we’ll see how it holds up

---

<div class="post-metadata">

### Author: ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)
#### Post date: [August 7, 2026, 11:00pm UTC](https://forum.kirupa.com/t/coding-challenge-3-find-duplicate-values/682967/7 "2026-08-07T23:00:17Z")

</div>

**Challenge solution:** Track how many times each value has been seen with a Map, and push a value into the result array at the exact moment its count first becomes 2, giving the order of first repeat.

**One way to do it:**

```js
function findDuplicates(arr) {
  const counts = new Map();
  const result = [];
  for (const val of arr) {
    const count = (counts.get(val) || 0) + 1;
    counts.set(val, count);
    if (count === 2) {
      result.push(val);
    }
  }
  return result;
}

```

**Why:**  
Each element is counted as it is encountered, and a value is added to the result only once, exactly when its occurrence count transitions from 1 to 2, which naturally captures the order in which values first repeat. Using a Map avoids duplicate entries and keeps the logic O(n).

* * *

**Got it:** @kirupa 🏆

**First-answer leaderboard**

1. @kirupa - 3 (firsts) 🏆
2. @emmawalter5 - 1 (first)
