+ 5

Python Counter equivalent in other programming language?

Python has a class called Counter which is very useful especially in natural language processing (NLP), information retrieval (IR), and text mining (TM) problems. It finds the frequency of items in a collection. For example: >>> from collections import Counter >>> # Use a string as an argument >>> Counter("Mississippi") Counter({'i': 4, 's': 4, 'p': 2, 'm': 1}) We can implement this class using a defaultdict like: >>> from collections import defaultdict >>> counter = defaultdict(int) >>> word = "mississippi" >>> for letter in word: counter[letter] += 1 >>> counter defaultdict(<class 'int'>, {'m': 1, 'i': 4, 's': 4, 'p': 2}) or we can use a simple dictionary as the following: >>> counter = {} >>> word = "mississippi" >>> for letter in word: counter[letter] = counter.get(letter, 0) + 1 >>> counter {'m': 1, 'i': 4, 's': 4, 'p': 2} I want to know is there any built-in class in other programming languages like C, C++, C#, Java, Javascript, Typescript, etc.?

4th Feb 2022, 9:19 PM
Behzad Soleimani Neysiani
Behzad Soleimani Neysiani - avatar
8 Réponses
+ 9
In most other languages, a frequency counter is not part of standard library, but very easy to implement using a HashMap / Dictionary. https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements https://stackoverflow.com/questions/29122394/word-frequency-count-java-8 As another example, Kotlin has standard collections method "eachCount" that makes this really simple. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/each-count.html#
5th Feb 2022, 3:01 AM
Tibor Santa
Tibor Santa - avatar
5th Feb 2022, 4:41 AM
Tibor Santa
Tibor Santa - avatar
+ 4
I found a Code Coach example called "Deja Vu" which can be solved using this collection. :-) I know that we can implement this collection easily in other programming languages (PL), but I want to know whether another PL has it built-in or not. Besides, we can implement it easily in C# using Linq (I add this implementation for C# users here): string word = "Mississippi"; var counter = word.GroupBy(x => x) .ToDictionary(g => g.Key, g => g.Count()); Console.WriteLine(String.Join(",",counter));
5th Feb 2022, 9:29 AM
Behzad Soleimani Neysiani
Behzad Soleimani Neysiani - avatar
+ 3
"If you give me a nail I will have to build myself a hammer...." (C language idiom)
5th Feb 2022, 2:18 AM
HungryTradie
HungryTradie - avatar
+ 3
I don't think there is one in the standard library of most languages (i know C++ best, and intermediate Java and C#). You can obviously build upon something that's part of the standard library (the STL map is a good starting point, which is basically a dictionary/hash table). I encourage you to work out the logic yourself (it's a fantastic mini-project in whatever language you're using), but here's a huge tip: Initialise all counters to 0, and increment the counter for each character every time you encounter it. The characters become the keys and the counts their values. If you're extra "inspired" and want to build the hash table yourself too, it's just an array of linked lists. Evaluating the ASCII value can be a good hash function here. Lastly, I should mention that you can execute Python scripts from other languages (I've tried with C++), if you're doing this for something you're building. Look up on how to configure your IDE to do that. (I've used VS so feel free to ask me if you're using VS)
6th Feb 2022, 7:37 PM
Hiba al-Sayf
Hiba al-Sayf - avatar
+ 3
Thanks dear Hiba al-Sayf for your contribution. As you see, I implement it in two languages, but this post can be useful if everyone post an implementation for other languages. Besides it would be appreciated to post problems and examples which can be solved easily using this collection.
6th Feb 2022, 8:06 PM
Behzad Soleimani Neysiani
Behzad Soleimani Neysiani - avatar