Skip to content

Instantly share code, notes, and snippets.

@ChathuraGH
Created December 1, 2023 19:31
Show Gist options
  • Save ChathuraGH/e825e9846996a25be83f5f53d9406de4 to your computer and use it in GitHub Desktop.
Save ChathuraGH/e825e9846996a25be83f5f53d9406de4 to your computer and use it in GitHub Desktop.
//Split the string with the spread ... operator instead of .split(''):
'🌯🌯🍣🍻'.split('')
//=> ["\ud83c", "\udf2f", "\ud83c", "\udf2f", "\ud83c", "\udf63", "\ud83c", "\udf7b"]
//vs
[...'🌯🌯🍣🍻']
//=> ["🌯", "🌯", "🍣", "🍻"]
//vs
'🌯'.charAt(0)
//=> "\ud83c"
//Then reduce:
[...'🌯🌯🍣🍻'].reduce((m, c) => (m[c] = (m[c] || 0) + 1, m), {})
//=> {'🌯': 2, '🍣': 1, '🍻': 1}
//source
// https://stackoverflow.com/questions/19480916/count-number-of-occurrences-for-each-char-in-a-string
// https://stackoverflow.com/a/68402128/13861187
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment