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

C as the first programming language instead of Java

Lab 0: Environment Setup

Inheritance on-the-fly with Ruby on Rails

There is a great deal of fear that comes with caring for patients with COVID-19. T

Function Overloading in Rust

An introduction to Kanban🧑‍🏫

DAY 6: CONTINUATION

Phantasma: Calling a Smart Contract via ScriptBuilder()

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

General Interview Questions for Web Development Entry Roles

Inserting values to Binary Search Tree

What are the Activities Involved SDLC Model?

How to approach a Backtracking Problem — Sudoku example

Backtracking paradigm on a high level