Published
Edited
Jun 28, 2019
Insert cell
md`# HashMaps`
Insert cell
Insert cell
array = [ 2, 3, 5, 5, 2, 3, 6, 9, 9, 6, 1];
Insert cell
findSingleElementInArr = (arr) => {
let hash = {}
let singleElement = null;
//build our map and the number of instances
arr.map((data) => {
if(hash[data]){
hash[data] = 2;
} else {
hash[data] = 1;
}
})
//reduce iterations by half by using the keys of our hashmap
let optimized = Object.keys(hash)
optimized.map((data) => {
if(hash[data] > 1){
singleElement = hash[data]
}
})
return singleElement
}
Insert cell
findSingleElementInArr(array)
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more