How to Count Number of Occurrences of a Given Character in a String (pythonpips.com) 4 points by vaibhavmule 7y ago ↗ HN
[–] 11235813213455 7y ago ↗ That 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 [–] Someone 7y ago ↗ 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 ‘.’)
[–] Someone 7y ago ↗ 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 ‘.’)
[–] hannofcart 7y ago ↗ How did this make it to HN front-page? Curious. [–] minimaxir 7y ago ↗ The weekend HN algorithm is very weird and frustrating.
4 comments
[ 1.9 ms ] story [ 18.6 ms ] thread(On top of that, I think it’s buggy. It won’t count character occurrences if char is a special character such as ‘^’ or ‘.’)