Home > OS >  Compare duplicates in an array. List values that are contained and not contained in array
Compare duplicates in an array. List values that are contained and not contained in array

Time:10-20

I have these arrays, I want to list unique values. If a value in $arr2 is in $arr1 I want to list that out but only ONE time. If a value in $arr2 is NOT in $arr1 I want to list that out as well. I would like to also add the values in $arr2 that are not contained in $arr1 to a seperate array. This is written in powershell but if you have a solution/tips in any other language that is perfectly fine with me I will rewrite to powershell.

$arr1 = @(1,1,1,2,2,3,4,4,5,7) 
$arr2= @(1,2,3,4,5,6,7) 

for ($i = 0; $i -lt $arr2.length; $i  ){
    for( $j = $i 1; $j -lt $arr1.length; $j   ){ 
        if($arr2[$i] -eq $arr1[$j]){
            Write-Host $arr2[$i]
        }
}
}

CodePudding user response:

You can use something like this to create an object which can be used for your need, however it will require further code from your side (i.e.: filtering) to get the results you're looking for.

$arr1 = @(1,1,1,2,2,3,4,4,5,7)
$arr2 = @(1,2,3,4,5,6,7)
$result = [System.Collections.Generic.List[pscustomobject]]::new()

foreach($i in $arr1   $arr2)
{
    if($i -in $result.Value)
    {
        continue
    }

    $z = [ordered]@{
        Value = $i
        Array1 = $false
        Array2 = $false
    }

    if($i -in $arr1)
    {
        $z.Array1 = $true
    }

    if($i -in $arr2)
    {
        $z.Array2 = $true
    }

    $result.Add([pscustomobject]$z)
}

$valuesOnBothArrays, $valuesOnOneArray = $result.Where({
    $_.Array1 -eq $true -and $_.Array2 -eq $true}, 'Split'
)
  • $valuesOnBothArrays will result in:
Value Array1 Array2
----- ------ ------
    1   True   True
    2   True   True
    3   True   True
    4   True   True
    5   True   True
    7   True   True
  • $valuesOnOneArray will result in:
Value Array1 Array2
----- ------ ------
    6  False   True

CodePudding user response:

I suggest using the Compare-Object cmdlet in combination with the .Where() array method:

$arr1 = 1,1,1,2,2,3,4,4,5,7
$arr2= 1,2,3,4,5,6,7

$inBoth, $uniqueRight = (Compare-Object -IncludeEqual $arr1 $arr2).
  Where({ $_.SideIndicator -in '==', '=>' }).
  Where({ $_.SideIndicator -eq '==' }, 'Split')

"-- in both (distinct):"
$inBoth.InputObject | Select-Object -Unique

"-- unique to arr2"
$uniqueRight.InputObject

The above yields:

-- in both (distinct):
1
2
3
4
5
7
-- unique to arr2
6
  • Related