Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,693 questions

57,001 answers

573 users

How to create an ASCII frequency table from a string in Node.js

1 Answer

0 votes
function getASCIIFrequency(str) {
    const frequencyTable = new Array(128).fill(0);

    for (let i = 0; i < str.length; i++) {
        frequencyTable[str.charCodeAt(i)]++;
    }

    return frequencyTable.reduce((accumulator, count, index) => {
        if (count > 0) {
            accumulator[String.fromCharCode(index)] = count;
        }
        return accumulator;
    }, {});
}

const str = "javascript c c++ c# java python php";

const asciiFrequency = getASCIIFrequency(str);

console.log(asciiFrequency);



/*
run:
  
{
  ' ': 6,
  '#': 1,
  '+': 2,
  a: 4,
  c: 4,
  h: 2,
  i: 1,
  j: 2,
  n: 1,
  o: 1,
  p: 4,
  r: 1,
  s: 1,
  t: 2,
  v: 2,
  y: 1
}
           
*/

 



answered Oct 16, 2024 by avibootz

Related questions

1 answer 207 views
1 answer 212 views
1 answer 213 views
1 answer 210 views
1 answer 226 views
1 answer 213 views
1 answer 190 views
...