How to Count Number of Occurrences of a Given Character in a String
1–5 of 5 posts
Re: How to Count Number of Occurrences of a Given Character in a String
#2That method doesn't really make sense, you could easily reuse an existing one: e.g. in JS: str.match(RegExp(char, 'g'))?.length, or [...str].filter(c => c === char).length
Re: How to Count Number of Occurrences of a Given Character in a String
#3How did this make it to HN front-page? Curious.
Re: How to Count Number of Occurrences of a Given Character in a String
#4How did this make it to HN front-page? Curious.
The weekend HN algorithm is very weird and frustrating.
Re: How to Count Number of Occurrences of a Given Character in a String
#5That method doesn't really make sense, you could easily reuse an existing one: e.g. in JS: str.match(RegExp(char, 'g'))?.length, or [...str].filter(c => c === char).length
That call allocates a regular expression and, even ignoring that allocation, will struggle to be as fast as the count call.
(On top of that, I think it’s buggy. It won’t count character occurrences if char is a special character such as ‘^’ or ‘.’)