1

I need to count the characters from a to z in an array.

For example I have an array like this:

["max","mona"]

The desired result would be something like this:

a=2, m=2, n=1, o=1, x=1

It would be great, if anyone could help me :)

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
sln
  • 83
  • 1
  • 2
  • 9
  • Do you know how to do it for a single string? (search might help: [`[javascript] count characters`](http://stackoverflow.com/search?q=%5Bjavascript%5D+count+characters)) – Felix Kling Jun 13 '16 at 19:51

7 Answers7

4

You can use two forEach loops and return object

var ar = ["max", "mona"], o = {}

ar.forEach(function(w) {
  w.split('').forEach(function(e) {
    return o[e] = (o[e] || 0) + 1;
  });
});

console.log(o)

Or with ES6 you can use arrow function

var ar = ["max","mona"], o = {}

ar.forEach(w => w.split('').forEach(e => o[e] = (o[e] || 0)+1));
console.log(o)

As @Alex.S suggested you can first use join() to return string, then split() to return array and then you can also use reduce() and return object.

var ar = ["max", "mona"];

var result = ar.join('').split('').reduce(function(o, e) {
  return o[e] = (o[e] || 0) + 1, o
}, {});
console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
2

You can use just one forEach loop and return object

var ar = [ "bonjour", "coucou"], map = {};
ar.join("").split("").forEach(e => map[e] = (map[e] || 0)+1);
console.log(map);

Live Demo

https://repl.it/C17p

Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
1

I would do it like this;

var     a = ["max","mona"],
charCount = a.reduce((p,w) => w.split("").reduce((t,c) => (t[c] ? t[c]++: t[c] = 1,t),p),{});
console.log(charCount);
Redu
  • 25,060
  • 6
  • 56
  • 76
1
public static void main (String[] args) throws java.lang.Exception
{
    String[] original = {"The","Quick","Brown","Fox","Jumps","Over","The","Lazy","Dog"};
    String singleString ="";
    for(String str : original )
    {
        singleString += str;
    }
     System.out.println(singleString);
    char[] chars = singleString.toLowerCase().toCharArray();
    Arrays.sort(chars);
    String result="";

    for(int i=0;i<chars.length;)
    {
    result += chars[i]+"=";
        int count=0;
        do {
            count++;
            i++;
            } while (i<chars.length-1 && chars[i-1]==chars[i]);

        result += Integer.toString(count)+",";

    }
    System.out.println(result.substring(0,result.length()-1));
}
1

Convert your array to a string using the join method and then use the length property of a string-

like:

arr.join('').length
moken
  • 3,227
  • 8
  • 13
  • 23
0

The solution using Array.join, Array.sort and String.split functions:

var arr = ["max","mona"],
    counts = {};

arr = arr.join("").split(""); // transforms the initial array into array of single characters
arr.sort();
arr.forEach((v) => (counts[v] = (counts[v])? ++counts[v] : 1));

console.log(counts);  // {a: 2, m: 2, n: 1, o: 1, x: 1}
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
-1

Try this:

var words = ['max', 'mona'],
    output = {};
    words.forEach(function(word){ 
    for(i=0; i < word.split('').length; i++){
    if(output[word[i]])
      output[word[i]] += 1;
    else{
      output[word[i]] = 1;
    }  
  } 
});

ps: sorry for the code not being formatted, I'm still getting used to the editor =)

Ehsan
  • 12,655
  • 3
  • 25
  • 44
n0m4d
  • 832
  • 6
  • 12