Episode 7 — DSA with JavaScript / 7.11 — Hashing Set and Map

7.11 — Exercise Questions: Hashing

<< Overview


E1. Two Sum

Solution (JS)
function ts(a,t){const m=new Map();for(let i=0;i<a.length;i++){const c=t-a[i];if(m.has(c))return[m.get(c),i];m.set(a[i],i);}return null;}

E2. Contains Duplicate

Solution (JS)
function cd(a){return new Set(a).size!==a.length;}

E3. Intersection

Solution (JS)
function inter(a,b){const s=new Set(a);return[...new Set(b.filter(x=>s.has(x)))];}

E4. Group Anagrams

Solution (JS)
function ga(s){const m=new Map();for(const w of s){const k=[...w].sort().join('');m.set(k,[...(m.get(k)||[]),w]);}return[...m.values()];}

E5. Longest Consecutive

Solution (JS)
function lc(n){const s=new Set(n);let mx=0;for(const x of s)if(!s.has(x-1)){let l=1,c=x;while(s.has(c+1)){c++;l++;}mx=Math.max(mx,l);}return mx;}

E6. Valid Anagram

Solution (JS)
function va(a,b){if(a.length!==b.length)return false;const f={};for(const c of a)f[c]=(f[c]||0)+1;for(const c of b){if(!f[c])return false;f[c]--;}return true;}

E7. Subarray Sum K

Solution (JS)
function ss(a,k){const m=new Map([[0,1]]);let s=0,c=0;for(const n of a){s+=n;if(m.has(s-k))c+=m.get(s-k);m.set(s,(m.get(s)||0)+1);}return c;}

E8. Top K Frequent

Solution (JS)
function topK(a,k){const f={};for(const x of a)f[x]=(f[x]||0)+1;return Object.entries(f).sort((a,b)=>b[1]-a[1]).slice(0,k).map(e=>+e[0]);}

E9. First Non-Repeating

Solution (JS)
function fnr(s){const f={};for(const c of s)f[c]=(f[c]||0)+1;for(const c of s)if(f[c]===1)return c;return null;}

E10. Find Duplicates

Solution (JS)
function fd(a){const s=new Set(),d=[];for(const x of a){if(s.has(x))d.push(x);else s.add(x);}return d;}

E11. Isomorphic Strings

Solution (JS)
// Map chars from s to t and verify bijection

E12. Word Pattern

Solution (JS)
// Map word to pattern char and verify

E13. Happy Number

Solution (JS)
// Use set to detect cycle

Additional exercise bank

EX-014

  • Problem: Exercise #14.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-015

  • Problem: Exercise #15.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-016

  • Problem: Exercise #16.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-017

  • Problem: Exercise #17.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-018

  • Problem: Exercise #18.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-019

  • Problem: Exercise #19.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-020

  • Problem: Exercise #20.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-021

  • Problem: Exercise #21.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-022

  • Problem: Exercise #22.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-023

  • Problem: Exercise #23.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-024

  • Problem: Exercise #24.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-025

  • Problem: Exercise #25.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-026

  • Problem: Exercise #26.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-027

  • Problem: Exercise #27.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-028

  • Problem: Exercise #28.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-029

  • Problem: Exercise #29.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-030

  • Problem: Exercise #30.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-031

  • Problem: Exercise #31.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-032

  • Problem: Exercise #32.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-033

  • Problem: Exercise #33.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-034

  • Problem: Exercise #34.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-035

  • Problem: Exercise #35.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-036

  • Problem: Exercise #36.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-037

  • Problem: Exercise #37.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-038

  • Problem: Exercise #38.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-039

  • Problem: Exercise #39.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-040

  • Problem: Exercise #40.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-041

  • Problem: Exercise #41.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-042

  • Problem: Exercise #42.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-043

  • Problem: Exercise #43.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-044

  • Problem: Exercise #44.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-045

  • Problem: Exercise #45.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-046

  • Problem: Exercise #46.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-047

  • Problem: Exercise #47.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-048

  • Problem: Exercise #48.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-049

  • Problem: Exercise #49.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-050

  • Problem: Exercise #50.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-051

  • Problem: Exercise #51.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-052

  • Problem: Exercise #52.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-053

  • Problem: Exercise #53.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-054

  • Problem: Exercise #54.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-055

  • Problem: Exercise #55.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-056

  • Problem: Exercise #56.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-057

  • Problem: Exercise #57.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-058

  • Problem: Exercise #58.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-059

  • Problem: Exercise #59.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-060

  • Problem: Exercise #60.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-061

  • Problem: Exercise #61.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-062

  • Problem: Exercise #62.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-063

  • Problem: Exercise #63.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-064

  • Problem: Exercise #64.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-065

  • Problem: Exercise #65.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-066

  • Problem: Exercise #66.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-067

  • Problem: Exercise #67.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-068

  • Problem: Exercise #68.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-069

  • Problem: Exercise #69.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-070

  • Problem: Exercise #70.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-071

  • Problem: Exercise #71.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-072

  • Problem: Exercise #72.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-073

  • Problem: Exercise #73.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-074

  • Problem: Exercise #74.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-075

  • Problem: Exercise #75.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-076

  • Problem: Exercise #76.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-077

  • Problem: Exercise #77.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-078

  • Problem: Exercise #78.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-079

  • Problem: Exercise #79.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-080

  • Problem: Exercise #80.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-081

  • Problem: Exercise #81.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-082

  • Problem: Exercise #82.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-083

  • Problem: Exercise #83.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-084

  • Problem: Exercise #84.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-085

  • Problem: Exercise #85.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-086

  • Problem: Exercise #86.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-087

  • Problem: Exercise #87.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-088

  • Problem: Exercise #88.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-089

  • Problem: Exercise #89.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-090

  • Problem: Exercise #90.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-091

  • Problem: Exercise #91.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-092

  • Problem: Exercise #92.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-093

  • Problem: Exercise #93.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-094

  • Problem: Exercise #94.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-095

  • Problem: Exercise #95.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-096

  • Problem: Exercise #96.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-097

  • Problem: Exercise #97.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-098

  • Problem: Exercise #98.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-099

  • Problem: Exercise #99.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-100

  • Problem: Exercise #100.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-101

  • Problem: Exercise #101.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-102

  • Problem: Exercise #102.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-103

  • Problem: Exercise #103.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-104

  • Problem: Exercise #104.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-105

  • Problem: Exercise #105.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-106

  • Problem: Exercise #106.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-107

  • Problem: Exercise #107.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-108

  • Problem: Exercise #108.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-109

  • Problem: Exercise #109.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-110

  • Problem: Exercise #110.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-111

  • Problem: Exercise #111.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-112

  • Problem: Exercise #112.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-113

  • Problem: Exercise #113.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-114

  • Problem: Exercise #114.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-115

  • Problem: Exercise #115.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-116

  • Problem: Exercise #116.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-117

  • Problem: Exercise #117.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-118

  • Problem: Exercise #118.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-119

  • Problem: Exercise #119.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-120

  • Problem: Exercise #120.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-121

  • Problem: Exercise #121.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-122

  • Problem: Exercise #122.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-123

  • Problem: Exercise #123.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-124

  • Problem: Exercise #124.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-125

  • Problem: Exercise #125.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-126

  • Problem: Exercise #126.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-127

  • Problem: Exercise #127.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-128

  • Problem: Exercise #128.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-129

  • Problem: Exercise #129.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-130

  • Problem: Exercise #130.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-131

  • Problem: Exercise #131.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-132

  • Problem: Exercise #132.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-133

  • Problem: Exercise #133.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-134

  • Problem: Exercise #134.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-135

  • Problem: Exercise #135.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-136

  • Problem: Exercise #136.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-137

  • Problem: Exercise #137.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-138

  • Problem: Exercise #138.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-139

  • Problem: Exercise #139.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-140

  • Problem: Exercise #140.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-141

  • Problem: Exercise #141.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-142

  • Problem: Exercise #142.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-143

  • Problem: Exercise #143.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-144

  • Problem: Exercise #144.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-145

  • Problem: Exercise #145.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-146

  • Problem: Exercise #146.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-147

  • Problem: Exercise #147.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-148

  • Problem: Exercise #148.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-149

  • Problem: Exercise #149.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-150

  • Problem: Exercise #150.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-151

  • Problem: Exercise #151.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-152

  • Problem: Exercise #152.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-153

  • Problem: Exercise #153.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-154

  • Problem: Exercise #154.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-155

  • Problem: Exercise #155.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-156

  • Problem: Exercise #156.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-157

  • Problem: Exercise #157.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-158

  • Problem: Exercise #158.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-159

  • Problem: Exercise #159.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-160

  • Problem: Exercise #160.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-161

  • Problem: Exercise #161.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-162

  • Problem: Exercise #162.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-163

  • Problem: Exercise #163.
  • Difficulty: Hard
  • Implementation: Both JS and C++.