Build a Map of element → count; query the map
When to useAnagram check, character frequency, finding most/least common
iterative
Character Counting
character_countingIs Anagram
is_anagramUsing Built-in Hash Tables
using_builtin_hash_tablesCount Frequencies
count_frequenciesValid Anagram
valid_anagramFirst Unique Character
first_unique_characterIntersection of Arrays
intersection_of_arraysDebt Records | Smallest Negative Balance
debt_recordsSplit Strings
google_oa_split_stringsGroup Anagrams
group_anagramsIsomorphic String
isomorphic_stringMost Common Word with Exclusion List
most_common_wordTransaction Logs
transaction_logsK-different Pairs in an Array
twitter_oa_k_differencePartition Array
twitter_oa_partition_arrayPick Up Coupons
google_oa_pick_up_couponsSubarray Sum Divisible by K
subarray_sum_divisibleNearest Cities
amazon_oa_nearest_citiesFavorite Genres
favorite_genresLabeling System
labeling_system · no slotsRoll Dice
roll_diceMax Network Rank | Codility
max_network_rankRansom Noteeasy
ransom_note · no slotsINITIALIZE
empty Map
const counts = {};
if (s.length !== t.length) return false;const counts = new Map();
const counts = new Map();
const freq = {};
if (s.length !== t.length) return false;const counts = new Map();
const counts = new Map();
const count1 = new Map();for (const num of nums1) { count1.set(num, (count1.get(num) || 0) + 1); }const result = [];
const balances = new Map();
const counts = new Map();for (const ch of s) { counts.set(ch, (counts.get(ch) ?? 0) + 1); }const left = new Map();let goodSplits = 0;
const groups = new Map();
if (s.length !== t.length) return false;const sToT = new Map();const tToS = new Map();
const bannedSet = new Set(banned);const counts = new Map();
const counts = new Map();
const counts = new Map();for (const n of nums) {counts.set(n, (counts.get(n) ?? 0) + 1);}
if (marbles.length % k !== 0) return false;const counts = new Map();
const lastSeen = new Map();let best = Infinity;
const counts = new Map([[0, 1]]);let prefix = 0;let result = 0;
const xMap = new Map(); const yMap = new Map(); const indexByName = new Map();
const songToGenre = new Map();for (const [genre, songs] of Object.entries(songGenres)) {for (const song of songs) songToGenre.set(song, genre);}const result = {};
—
const counts = new Array(7).fill(0);for (const d of dice) counts[d]++;
const degree = new Map(); let maxRank = 0;
—
TRAVERSE
for-of input
for (const ch of s) {
for (const ch of s) {
for (const word of words) {
for (const num of arr) {
for (const char of s) {
for (const ch of s) {
for (const num of nums2) {
for (const [debtor, creditor, amount] of transactions) {
for (let i = 0; i < s.length - 1; i++) {
for (const s of strs) {
for (let i = 0; i < s.length; i++) {
for (const w of words) {
for (const log of logs) {
for (const [n, c] of counts) {
for (const m of marbles) {
for (let i = 0; i < card.length; i++) {
for (const value of arr) {
for (let i = 0; i < cities.length; i++) {
for (const [user, songs] of Object.entries(userSongs)) {
—
for (let face = 1; face <= 6; face++) {
for (let i = 0; i < starts.length; i++) {
—
[COUNT]
increment count for each element
counts[ch] = (counts[ch] || 0) + 1;
counts.set(ch, (counts.get(ch) ?? 0) + 1);
counts.set(word, (counts.get(word) || 0) + 1);
freq[num] = (freq[num] || 0) + 1;
counts.set(char, (counts.get(char) || 0) + 1);
counts.set(ch, (counts.get(ch) ?? 0) + 1);
if ((count1.get(num) || 0) > 0) {result.push(num);count1.set(num, count1.get(num) - 1);}
balances.set(debtor, (balances.get(debtor) ?? 0) - amount);balances.set(creditor, (balances.get(creditor) ?? 0) + amount);
const ch = s[i];left.set(ch, (left.get(ch) ?? 0) + 1);counts.set(ch, counts.get(ch) - 1);if (counts.get(ch) === 0) counts.delete(ch);
const key = [...s].sort().join('');if (!groups.has(key)) groups.set(key, []);groups.get(key).push(s);
const a = s[i]; const b = t[i];if (sToT.has(a) && sToT.get(a) !== b) return false;if (tToS.has(b) && tToS.get(b) !== a) return false;sToT.set(a, b); tToS.set(b, a);
if (bannedSet.has(w)) continue;counts.set(w, (counts.get(w) ?? 0) + 1);
const [a, b] = log.split(' ');counts.set(a, (counts.get(a) ?? 0) + 1);if (a !== b) counts.set(b, (counts.get(b) ?? 0) + 1);
if (k === 0) {if (c >= 2) pairs++;} else {if (counts.has(n + k)) pairs++;}
counts.set(m, (counts.get(m) || 0) + 1);
if (lastSeen.has(card[i])) {best = Math.min(best, i - lastSeen.get(card[i]) + 1);}
prefix += value;let mod = prefix % k;if (mod < 0) mod += k;result += counts.get(mod) ?? 0;counts.set(mod, (counts.get(mod) ?? 0) + 1);
if (!xMap.has(x[i])) xMap.set(x[i], []);xMap.get(x[i]).push(i);if (!yMap.has(y[i])) yMap.set(y[i], []);yMap.get(y[i]).push(i);indexByName.set(cities[i], i);
const counts = new Map();for (const song of songs) {const genre = songToGenre.get(song);if (genre) counts.set(genre, (counts.get(genre) ?? 0) + 1);}
—
let rotations = 0;for (let d = 1; d <= 6; d++) {if (d === face) continue;rotations += (d + face === 7) ? counts[d] * 2 : counts[d];}
degree.set(starts[i], (degree.get(starts[i]) ?? 0) + 1);degree.set(ends[i], (degree.get(ends[i]) ?? 0) + 1);
—
query
iterate map to derive answer
—
for (const ch of t) {if (!counts.has(ch) || counts.get(ch) === 0) return false;counts.set(ch, counts.get(ch) - 1);}
—
—
—
for (let i = 0; i < s.length; i++) {if (counts.get(s[i]) === 1) return i;}
// result collected during traverse
for (const [name, balance] of balances) { if (balance < 0) result.push(name); }
if (left.size === counts.size) goodSplits++;
// groups.values() holds all anagram clusters
// mapping consistency verified inline during TRAVERSE
let best = ''; let bestCount = 0;for (const [word, c] of counts) {if (c > bestCount) { best = word; bestCount = c; }}
const flagged = [];for (const [id, count] of counts) {if (count > threshold) flagged.push(id);}flagged.sort((a, b) => Number(a) - Number(b));
// pairs accumulates during traversal
for (const count of counts.values()) { if (count > k) return false; }
// answer already accumulated in best
// result accumulates during traversal
for (const q of queries) { /* look up candidates, find min-distance */ }
let maxCount = 0;for (const c of counts.values()) maxCount = Math.max(maxCount, c);result[user] = [...counts.entries()].filter(([, c]) => c === maxCount).map(([g]) => g);
—
best = Math.min(best, rotations);
for (let i = 0; i < starts.length; i++) {const rank = degree.get(starts[i]) + degree.get(ends[i]) - 1;if (rank > maxRank) maxRank = rank;}
—
RETURN
result
return counts;
return true;
return counts;
return freq;
return true;
return -1;
return result;
return result;
return goodSplits;
return [...groups.values()];
return true;
return best;
return flagged;
return pairs;
return true;
return best === Infinity ? -1 : best;
return result;
return result;
return result;
—
return best;
return maxRank;
—