Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Sort an array alphabetically and return a number representation for every character

I want to try to make my own function. I want to sort an array of characters in increasing order and return the occurrence count of each character as a compact number.

Example:

function numberRepresentation(arr) {
    // arr is an array
    // return a number

    /*
    ex. arr=[b,a,a,a,c,b,a]
    after sorting array will look like arr=[a,a,a,a,b,b,c]
    occurrence of characters -> a = 4, b = 2, c = 1 
    so output will be 421
    */
}

How can I do it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function numberRepresentation(arr) {
    return arr.reduce((counts, letter) => {
        const code = letter.charCodeAt(0)
        counts[code] = counts[code] + 1 || 1
        return counts
    }, []).join('')
}

Explanation:

We just count occurrence of every letter, but we use a simple Array and char codes as indexes. Thus we automatically get a list of counts of letters ordered alphabetically (because the char codes go in abc order). So all we need to do is concatenate all the counts and return.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!