Skip to content

Instantly share code, notes, and snippets.

@lvngd
Created March 6, 2021 18:28
Show Gist options
  • Save lvngd/bc3d57a711cee3a423d4099d5ecc54fd to your computer and use it in GitHub Desktop.
Save lvngd/bc3d57a711cee3a423d4099d5ecc54fd to your computer and use it in GitHub Desktop.
let words = ['cat','dog','mouse','rabbit','dog','dog','mouse','cat'];
let wordsSet = new Set(words)
let wordMap = new Map();
for(word in words){
let current = words[word];
if (wordMap.has(current)){
//is in the map, so get the current value and increment by one
let currentVal = wordMap.get(current);
currentVal++;
//update the value in the map
wordMap.set(current, currentVal);
}
else{
//not in the map, set its value to 1
wordMap.set(current,1)
}
}
//test to see if the size of the map is the same as the size of the set
let sameSizeTest = wordMap.size == wordsSet.size;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment