Created
December 1, 2023 19:31
-
-
Save ChathuraGH/e825e9846996a25be83f5f53d9406de4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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