How To Find Duplicates In An Array Using PowerShell

Welcome to an article where we will find the duplicates in an array. Sometimes, we need to clean the array and get the duplicate values. Here, I have a simple script for you to run with minimal inputs from your end to identify the duplicates on your array.

Let’s see how can we do it.

Steps

  1. Open Windows PowerShell Modules as an administrator.



  2. Paste the code, mentioned below as .ps1 file and execute it.
    1. #Add the PowerShell snap in code  
    2. Add - PSSnapIn Microsoft.SharePoint.PowerShell - ErrorAction SilentlyContinue | Out - Null  
    3. $ht = @ {}  
    4. $Array | foreach {  
    5.     $ht["$_"] += 1  
    6. }  
    7. $ht.keys | where {  
    8.     $ht["$_"] - gt 1  
    9. } | foreach {  
    10.     write - host "Duplicate found $_"  
    11.     $newarray += $_  
    12. }  
    13. foreach($obj in $newarray) {  
    14.         foreach($term in $terms) {  
    15.             if ($term.Labels[1].Value - eq $obj) {  
    16.                 $emailarray += $term.Name  
    17.                 write - host $emailarray  
    18.             }  
    19.         } 

Pre-Requisites

  1. $Array
    Assign the array having duplicate values, where you want to find the duplicate values. The script, mentioned above, will compare the array and find duplicates and results with them in the $emailarray as an output.

    The script is so quick that it will hardly take any effort and time.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now