Powershell: How to return a number of duplicates in an array list

How do I find the number of duplicate items in an array list?

Quick Post showing how to iterate through and array list in Powershell and then return a number of duplicate objects stored. Basically compares the current object to the next object. Once it has a match it continues looking for the next different object in the list and then adds a 1 to the count.

You could create an array in the else if to store the object to a new array list of duplicated objects.

<#Sort the array list of values for finding duplicates#>

<#Create variables to store iterator and number of duplicates found #> 
$NumberOfDuplicates = 0

<# This for loop goes through each Array List object and checks for duplicates #>
for($i=0; $i -le $ArrayListAlpha.Count; $i++){ 
    <# If the current array list object matches the next add one to match itterator j and move to next array list item#> 
    if ($ArrayListAlpha[$i] -eq $ArrayListAlpha[$i+1]){
    <# if the current array list object doesnt match the next then check if there was a previous match and  add one to the number of duplicates and reset the match counter itterator j #> 
    }elseif ($j -gt 0){

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s