Skip to content

Instantly share code, notes, and snippets.

@lvngd
Created March 6, 2021 18:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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