JavaScript How to group repeated characters by number of the times those got repeated?

Objective:

Load a variable with a string value with random characters, split each character into an array, sort those alphabetically and also made them in upper case format. At end, it's required print the number times each character was repeated and store the results into a new variable in a string data.

Example:

const randomChars = 'ABaaBCDdeGFAAR';

Desired Output:

5A 2B 1C 2D 1E 1F 1G 1R

My attempt to solve the problem:

// convert string to uppercase:
let upperChars = randomChars.toUpperCase();
// convert characters into array:
let splitChars = upperChars.split("");
// order the chars by alphabetic order in the array:
let sortChars = splitChars.sort(); 

Now I am stuck the part where I have to group the characters, any idea how can I solve this?

Is there anyway to ignore the white space in the split function in case some string has space in between characters? ex: "ASdj asd asADASD"

Answer:1

Try (we use characters as keys in h={} and count them)

const randomChars = 'ABaaBCDdeGFAAR';

let h= {};
randomChars.toUpperCase().split('').forEach(c => h[c]=++h[c]||1);
let r = Object.keys(h).sort().map(k => h[k]+k).join(' ');

console.log(r);
Answer:2

Use Map

const randomChars = 'ABaaBCDdeGFAAR';
let result = new Map();
for(let letter of randomChars.toUpperCase().sort().replace(/ /,'').split('')) {
    if (result.has(letter)) {
        result.set(letter, result.get(letter)+1);
    } else {
        result.set(letter, 1);
    }
}
let str = ""
for(let pair of result) {
     str += pair.join('')
}
console.log(str)
Answer:3

To achieve expected result, use below option of using reduce with Object.entries

  1. Split string with '' and use reduce to loop string
  2. Convert each character to upper case and count each character into accumulator obj
  3. Use Object.entries to join count with character

const randomChars = 'ABaaBCDdeGFAAR';
let obj = randomChars.split('').reduce((acc,v)=> {
  v = v.toUpperCase();
  acc[v] = acc[v]?acc[v] +1 : 1;
  return acc
}, {})
//{A: 5, B: 2, C: 1, D: 2, E: 1, F: 1, G: 1, R: 1}

console.log(Object.entries(obj).map((v) => v[1] + v[0]))
Answer:4

I have a DOM as shown below in which I want to hide specific h2 tag contents. All of them have the same class shows-list__title. The contents are at Line A (Vote 2015 Phone-in), Line B(Vote 2015 ...

I have a DOM as shown below in which I want to hide specific h2 tag contents. All of them have the same class shows-list__title. The contents are at Line A (Vote 2015 Phone-in), Line B(Vote 2015 ...

  1. hide html content using jquery
  2. hide html content
  3. hide html content using javascript
  4. hide html content css
  5. hide html content using css
  6. html hide content on click
  7. hide content html code
  8. html hide content on mobile
  9. html hide content outside div
  10. html hide content of div
  11. html hide content behind div
  12. html hide content inside div
  13. html hide content button
  14. php hide html content
  15. hide show content html
  16. hide table content html
  17. html hide td content
  18. html hide tab content
  19. html hide page content
  20. wap to hide html content on button click

I have the code shown here: https://codepen.io/allen-houng/pen/ExYKYdq and a gist here: <div class="parent"> <div class="imageWrapper"> <img class="image" src="imageurl" /> &...

I have the code shown here: https://codepen.io/allen-houng/pen/ExYKYdq and a gist here: <div class="parent"> <div class="imageWrapper"> <img class="image" src="imageurl" /> &...

Here I am mapping financialTransactionDetail array and populating data. Values are coming fine. Now I have to get the sum of TransactionAmount, like the length of array is 3 there are 3 ...

Here I am mapping financialTransactionDetail array and populating data. Values are coming fine. Now I have to get the sum of TransactionAmount, like the length of array is 3 there are 3 ...

  1. find array element matlab
  2. find array element javascript
  3. find array element mongodb
  4. find array element in java
  5. find array element python
  6. find array element in another array javascript
  7. find array element in php
  8. find array element in array
  9. find array element by value php
  10. find array element js
  11. find array element in jquery
  12. find array element in c
  13. find array element typescript
  14. find array element index matlab
  15. find array element position javascript
  16. find array element position php
  17. find array element index python
  18. find array element vba
  19. find array element index java
  20. find array element lodash

At the moment when 'scroll' is at the very bottom, the function getUsers () is called. How to set the scroll so that it doesn't reach the end of the slider, and the getUsers () function is called. ...

At the moment when 'scroll' is at the very bottom, the function getUsers () is called. How to set the scroll so that it doesn't reach the end of the slider, and the getUsers () function is called. ...