DEV Community

Discussion on: Javascript Algorithms Challenges | Part 2

Collapse
 
kepta profile image
Kushan Joshi • Edited

My code golf to get the maxCharacter

const maxCharacter = string =>
  [...string]
    .map(char => [char, string.split(char).length])
    .reduce(
      ([maxChar, maxOcc], [c, o]) => (maxOcc < o ? [c, o] : [maxChar, maxOcc])
    );

maxCharacter('lollipop'); // ['l', 4]

I agree it is terse, but if you love coding wizardry you might see the beauty in it <3. (The secret is to avoid variables and keeping it simple)