Basic Algorithm Solving Strategies: Hash Maps

After reading this blog, you will definitely be able to use a hash map better than this guy can use a regular map!
containsDuplicate = (arr) => {
if (arr.length === 0) {
return false
}
let numHash = {}

for (let i = 0; i < arr.length; i++) {
if (numHash[arr[i]]) {
numHash[arr[i]] += 1
} else {
numHash[arr[i]] = 1
}
}
for (let k in numHash) {
if (numHash[k]> 1) {
return true
}
}

return false
}
firstUniqueCharacter = (s) => {
let sMap = {}

//hash map
for (let char of s) {
if (sMap[char]) {
sMap[char] += 1
} else {
sMap[char] = 1
}
}
for (let i = 0; i < s.length; i++) {
if (sMap[s[i]] === 1) {
return i
}
}

return -1
};
Honestly, go watch this video. it’s incredible.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

I was looking for a house, so I built a web scraper in Python!

Microservices and the Saga Pattern

Unreal Engine CI. Part 02. Blueprint Game Build

7 Interesting Java Project Ideas & Topics For Beginners [2022]

TDD — 5 IDE Helpers to Make Development Easier

Orbit Animation with Flutter

Systems Thinking episode #4: DSRP

LeetCode — Swap Nodes in Pairs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HopeGiometti

HopeGiometti

More from Medium

Introduction: What is a Hash Table

Rest API — What Is a Rest API?

Using Open Source for Career Advancement, with Community Classroom’s Kunal Kushwala

Tech Interview Pro Review — Is Patrick Shyu Course Worth It?

Tech Interview pro review