Created
August 17, 2023 15:17
-
-
Save suhailgupta03/f25ef4c5c318521ba2606f03e4debb68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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