Powershell to find duplicates in an Array just like Python's cool Counter class.

As we know Python comes with a lot of cool utility modules that helps devs to achieve more in less time one such thing is Counter class that would convert a list of duplicate elements into a dictionary with the count of how many times that element appeared in the list.


Article content

As we can see in just one line of code we have found duplicates with the count of their occurrences in the list.

So, how to achieve the same in powershell. Simple! convert the list to a hashtable [FYI , optimization is not the goal here.].

We are iterating over the given array and during the iteration check if the current item is already a key in the hashtable, if not then add to hashtable and assign a value of 1 and if it is already there then simply increment the value by 1.

Article content


Bharani Ganesh S

Delivery Leader at Kyndryl

3mo

I knew that it would not stop you from exploring 🤣

Bharani Ganesh S

Delivery Leader at Kyndryl

3mo

I know on why got to work on this. 🤣

To view or add a comment, sign in

More articles by Prakash Iyengar

  • Build your own AIOPS using open source tools

    In today’s day and age having right AIOPS strategy is must, in this article I have outlined different tools that we…

    1 Comment

Insights from the community

Others also viewed

Explore topics