First of all, what is frequency?
According to Wikipedia, it's" the rate at which something occurs over a particular period of time or in a given sample."
There are multiple ways to get the frequency of an element in an array. In this article, we'll focus on one of those ways by using some of the higher-order functions in Javascript.
Let's begin
For instance, if we have an array of letters:
const letters = ["a", "b", "c", "a", "b", "c", "a", "b"];
To get the frequency of each element, we will first need to create an empty object
named count
:
const count = {};
Next, we'll utilize one those higher order functions we were talking about:
letters.forEach(e => count[e] ? count[e]++ : count[e] = 1 );
What the above code does is simply check, if each element of letters
is present in count
. Initially count
is empty and does not contain any of letters
elements, so when the ternary operator runs on an element of letters
to check if its present in count
for the first time, it's going to work with the falsy condition which is to assign letters
elements to 1 in the count
object.
So the count object will then become:
console.log(count) // {"a" : 1, "b" : 1, "c" : 1};
Afterward, when the ternary operator checks if count
has an element of letters
that has already been checked, the ternary operator will then run the truthy condition which is to increment the value of the elements in letters
if they are already present in count
, so count
will now become:
console.log(count) // {"a" : 3, "b" : , "c" : 2};
And just like that ladies and gentlemen, we've been able to get the frequency of each element of letters
.
Conclusion
Here's the complete code:
const letters = ["a", "b", "c", "a", "b", "c", "a", "b"];
const count = {};
letters.forEach(e => count[e] ? count[e]++ : count[e] = 1 );
console.log(count) // {"a" : 1, "b" : 1, "c" : 1}
P.S: This code can be used for both strings and numbers.
Top comments (18)
There's no need for
nums.map(e => Number(e))
. Here, you are transforming numbers to... well, numbers. You can skip this step. If you really want to convert them to strings you should use eitherString(e)
ore.toString()
.I'm sorry It's a typo, I originally wanted to write String(e). Thanks for pointing out to me
You can change
Number(e)
with+e
Thats too hacky and non intuitive i.e. not readable. As a simple rule you must avoid being clever because readable code is always better than clever code 😊
True, simple code is good code.
Thanks for the pointer.
We could just use
Array.reduce
Yeah, there's more that one way to skin a cat
Thanks!
Wow thanks, that's awesome and shorter
no problems, keep learning !
Why would you need to do this any differently for numbers vs strings?? The first example (the one that you show as being "for string elements") works just fine if you pass in the array of numbers.
Wow, I never realized that. Thanks for the pointer, I'll the remove the 'For numbers' part.
Ngl the "for numbers" part is stupid
That I agree with, thanks for your feedback
Thanks for this. This is good for beginners like me as it says in the tags. Some of the other suggestions are cool too but a bit more confusing :)