Let’s assume I need a function to get the frequency of the vals of an array, the output should be an object of key value pairs representing how many times a val is repeated in the given arr, the order doesn’t matter. Example:
[1, 3, 4, 1] expected output: {1: 2, 3: 1, 4: 1}
[2, 3, 5, 3] expected output: {2: 1, 3: 2, 5: 1}
function getFrequency(arr) {
let freqCounter = {}
for(let val of arr) {
// I am confused as how this line of code works
freqCounter[val] = (freqCounter1[val] || 0) + 1;
//
}
return freqCounter;
}
How exactly is that line working, normally I would do it as followed:
for(let val of arr) {
freqCounter[val] = freqCounter[val] ? ++frequencyCounter[val] : 1;
}
Can someone explain to me how are those lines doing the same thing?
Oh and by the way, does anybody know of an online tool to measure algorithms time complexity (Big O)?