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.

--

--

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