Use Map to store array values and the number of repetitions

3396 views javascript

I'm learning JavaScript and I'm trying to resolve an exercise in which I have to create an array with the letters of my name, and then, using Map, store the values and the number of times that each value appears.

I've been reading about Map and I guess that I should start with something similar to this to check if the array has repetitions, but I'm a newbie and I don't find many Map examples, so I have no idea of what I'm doing…

  var myMap = new Map([[1, "M"], [2, "a"], [3, "r"], [4, "y"]]);

for (i = 1; i < 5; i++) {
console.log(myMap.has (i));
console.log(myMap.get (i));


Thank you!

answered question

You should start with an array of letters. The Map will be used to count accurences.

Not an answer, just a hint that "Mary".split("") returns ["M","a","r","y"]. May or may not be useful in your case :).

1 Answer


Start off with something like this:

var myName = ['M', 'a', 'r', 'y'];
var letterFrequencyMap = new Map();

for (var i = 0; i < myName.length; i++) {
    // Your logic to update letterFrequencyMap


posted this

Have an answer?


Please login first before posting an answer.