Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created August 17, 2023 15:17
Show Gist options
  • Save suhailgupta03/f25ef4c5c318521ba2606f03e4debb68 to your computer and use it in GitHub Desktop.
Save suhailgupta03/f25ef4c5c318521ba2606f03e4debb68 to your computer and use it in GitHub Desktop.
let listWithDuplicateValues = [1,1,1,1,2,3,4,5,6,5];
let frequencyMap = {};
/**
* {
* 1:4,
* 2: 1,
* 3: 1
* }
*/
for(let i = 0; i < listWithDuplicateValues.length; i++) {
let item = listWithDuplicateValues[i];
let itemFromFrequencyMap = frequencyMap[item];
if(itemFromFrequencyMap) { // this if statement will be true if the item
// is not undefined (which means that the item is already present in the frequency map)
frequencyMap[item] = itemFromFrequencyMap + 1;
// This means that the item is already present in the frequency map
// and we need to increment the frequency
}else {
frequencyMap[item] = 1; // This will be the initial frequency
}
}
console.log(frequencyMap)
@suhailgupta03
Copy link
Author

suhailgupta03 commented Aug 17, 2023

Line 12:

let itemFromFrequencyMap = frequencyMap[item]; // Here we are trying to get
// the item from the frequency map
// here item is the key and frequencyMap[item] is the value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment