Guest User

Untitled

a guest
Sep 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. const maxStringCounter = str => {
  2. let obj = {};
  3.  
  4. str.split("").forEach(el => {
  5. if (obj[el]) {
  6. return (obj[el] += 1);
  7. } else {
  8. return (obj[el] = 1);
  9. }
  10. });
  11.  
  12. const values = Object.values(obj);
  13. const keys = Object.keys(obj); //?
  14. const maxValue = Math.max(...values);
  15. const indexOfLargestValue = values.indexOf(maxValue);
  16.  
  17. return keys[indexOfLargestValue];
  18. };
  19.  
  20. maxStringCounter("44123"); // 4
  21. maxStringCounter("441111123"); // 1
Add Comment
Please, Sign In to add comment