Skip to content

Instantly share code, notes, and snippets.

@jonrandy
Last active July 16, 2024 08:31
Show Gist options
  • Save jonrandy/6103c00128faf009812dce26019b1860 to your computer and use it in GitHub Desktop.
Save jonrandy/6103c00128faf009812dce26019b1860 to your computer and use it in GitHub Desktop.
Most common char in string JS
const mostFrequent = s=>s.match(/(.)\1+/g)?.sort((a,b)=>b.length-a.length)[0][0]||s[0]
const mostFrequentLetter = s=>([...s].sort().join('').match(/(\w)\1+/g)?.sort((a,b)=>b.length-a.length)[0]||s.match(/\w/))[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment