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_iterationThe Lookup Problem
lookup_problemSets - Uniqueness
sets_uniquenessContains Duplicate
contains_duplicateWord Pattern II
word_pattern_iiLargest K such that both K and -K exist in array
largest_k_positive_and_negativeCreate String
google_oa_create_stringINITIALIZE
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;