counting frequency of characters in a string using javascript

I need to write some kind of loop that can count the frequency of each letter in a string.

For example: “aabsssd”

output: a:2, b:1, s:3, d:1

Also want to map same character as property name in object. Any good idea how to do this?

I am not sure how to do it.

This is where I am so far:

var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

function counter(x) {
    var count=0, temp = [];
    x = x.split('');
    console.log(x);
    for(var i=0, len = x.length; i < len; i++) {
        if(x[i] == "a") {
            count++;
        }
    }
    return count;
}
var a = "aabbddd";
console.log(counter(a));

45 thoughts on “counting frequency of characters in a string using javascript”

  1. First off I would like to say awesome blog! I had a quick question which I’d like to ask if you don’t mind.
    I was curious to know how you center yourself and clear your mind before writing.
    I’ve had a difficult time clearing my mind in getting my ideas out.
    I do enjoy writing but it just seems like the first 10 to 15 minutes
    tend to be lost simply just trying to figure out how to begin. Any recommendations
    or tips? Thanks!

    Reply
  2. Oh my goodness! Impressive article dude! Thanks, However I am having difficulties with your RSS.
    I don’t know the reason why I am unable to subscribe to it.
    Is there anybody getting identical RSS issues? Anyone that knows the
    answer can you kindly respond? Thanks!!

    Reply
  3. Hi there fantastic website! Does running a blog like this take a lot of work?
    I’ve absolutely no knowledge of coding but I had been hoping to start my own blog soon. Anyway, if you have
    any recommendations or tips for new blog owners please share.

    I understand this is off subject nevertheless I simply had to
    ask. Many thanks!

    Reply
  4. A person essentially assist to make seriously posts I might state.
    That is the first time I frequented your website page and to
    this point? I surprised with the research you made to make this
    particular put up extraordinary. Magnificent job!

    Reply
  5. Does your site have a contact page? I’m having a tough time
    locating it but, I’d like to send you an email. I’ve got
    some suggestions for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it
    improve over time.

    Reply
  6. Howdy are using WordPress for your blog platform? I’m new to the blog world but I’m trying
    to get started and create my own. Do you require any html coding expertise to make your own blog?
    Any help would be really appreciated!

    Reply
  7. Today, I went to the beach with my kids. I found a sea shell and
    gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

    Reply
  8. Hey there would you mind stating which blog platform you’re
    working with? I’m planning to start my own blog in the near future but I’m having a
    difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your layout seems different then most blogs and I’m looking for something completely
    unique. P.S Apologies for being off-topic but
    I had to ask!

    Reply
  9. I absolutely love your website.. Great colors & theme.

    Did you make this amazing site yourself? Please reply back as I’m
    attempting to create my very own blog and would like to know where
    you got this from or exactly what the theme is named. Cheers!

    Reply
  10. Just wish to say your article is as surprising.

    The clarity in your post is just spectacular and i can assume
    you’re an expert on this subject. Fine with
    your permission let me to grab your RSS feed to keep up
    to date with forthcoming post. Thanks a million and please carry on the rewarding work.

    Reply
  11. Hi there, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam comments?
    If so how do you protect against it, any plugin or anything you can advise?
    I get so much lately it’s driving me mad so any assistance
    is very much appreciated.

    Reply
  12. Hi there! I understand this is sort of off-topic but I had to ask.
    Does building a well-established website such as yours require a large amount of work?
    I am completely new to writing a blog but I do write in my journal daily.
    I’d like to start a blog so I can easily share my
    experience and views online. Please let
    me know if you have any suggestions or tips for new
    aspiring bloggers. Thankyou!

    Reply
  13. Hi! I simply would like to give you a big thumbs up for your excellent information you’ve got right here
    on this post. I will be returning to your site for more soon.

    Reply
  14. Woah! I’m really enjoying the template/theme of
    this blog. It’s simple, yet effective. A lot of times it’s difficult to get that
    “perfect balance” between usability and visual appeal.

    I must say you have done a great job with this. In addition, the blog loads
    extremely fast for me on Chrome. Excellent Blog!

    Reply

Leave a Comment