Fundamentals/Patterns/Hash Set — Membership Check7 problems· Hash Tables

Build a Set of seen items; check membership in O(1)

When to useDetect duplicates, set intersection/union, check if element exists

iterative
String Iteration
string_iteration
The Lookup Problem
lookup_problem
Sets - Uniqueness
sets_uniqueness
Contains Duplicate
contains_duplicate
Word Pattern II
word_pattern_ii
Largest K such that both K and -K exist in array
largest_k_positive_and_negative
Create String
google_oa_create_string
INITIALIZE
empty Set
const vowels = new Set(['a','e','i','o','u']);
let count = 0;
const set = new Set();
const seen = new Set();
const seen = new Set();
const numSet = new Set(nums);
let result = 0;
const positions = new Set(intList);
let result = '';
TRAVERSE
for-of input
for (const ch of s.toLowerCase()) {
for (const value of arr) {
for (const n of arr) {
for (const n of nums) {
for (const n of nums) {
for (let i = 0; i < n; i++) {
[CHECK]
check has(); add to set
if (vowels.has(ch)) count++;
set.add(value);
seen.add(n);
if (seen.has(n)) return true;
seen.add(n);
const word = s.slice(si, end);
if (usedWords.has(word)) continue;
if (n > 0 && numSet.has(-n)) result = Math.max(result, n);
result += positions.has(i) ? '+' : '-';
RETURN
result (often early exit on hit)
return count;
return set.has(target);
return [...seen];
return false;
return false;
return result;
return result;