bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to starter track
šŸŽÆCode CheckjavascriptEasyArrays & Hashing

Starter: notice a duplicate

Why check the set before treating the value as new? Goal: Check whether a value is already stored before treating it as new.

Starter step 8 of 20Move through arrays

A condition can be correct while the returned value is wrong.

Ask what should happen when a duplicate is found.

Problem Brief

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Puzzle Hints
  1. Why check the set before treating the value as new?

  2. A set checks membership with has before you decide it is a duplicate. The right choice is "The set can tell us whether 3 was already seen" because it preserves the goal: Check whether a value is already stored before treating it as new.

  3. Tie your answer back to Arrays & Hashing: what data is stored, when it updates, and what condition uses it?

Asked at 12 companies
AdobeAirbnbAmazon
Contains Duplicate — HashSet
hashmap
nums1231
1
5

Input: [1,2,3,1]. Use HashSet to detect duplicates

Map & Set in JavaScriptref

JavaScript provides Map for key-value pairs and Set for unique values. Both offer O(1) average lookup, insert, and delete. Unlike plain objects, Map preserves insertion order and allows any type as keys.

-new Map() — O(1) get/set/has/delete
-new Set() — O(1) add/has/delete, auto-deduplicates
-Map.get(key) returns undefined if missing
-for...of iterates Map entries as [key, value]
const map = new Map();
map.set('a', 1);    // set key-value
map.get('a');        // 1
map.has('a');        // true

const set = new Set([1, 2, 2, 3]);
set.size;            // 3 (deduped)
set.has(2);          // true
Official docs →
Map & Set in JavaScriptref

JavaScript provides Map for key-value pairs and Set for unique values. Both offer O(1) average lookup, insert, and delete. Unlike plain objects, Map preserves insertion order and allows any type as keys.

-new Map() — O(1) get/set/has/delete
-new Set() — O(1) add/has/delete, auto-deduplicates
-Map.get(key) returns undefined if missing
-for...of iterates Map entries as [key, value]
const map = new Map();
map.set('a', 1);    // set key-value
map.get('a');        // 1
map.has('a');        // true

const set = new Set([1, 2, 2, 3]);
set.size;            // 3 (deduped)
set.has(2);          // true
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
1// Goal: Check whether a value is already stored before treating it as new.
2function starterExample() {
3 const seen = new Set([3]);
4 if (seen.has(3)) {
5 return true;
6 }
7}

Why check the set before treating the value as new?