2

Possible Duplicate:
Comparing two arrays & get the values which are not common

I wanted a logic to get uncommon items from an array, for example:

$a=@(1,2,3,4,5,6)
$b=@(1,2,3,4,5,7,9,10)

I want the output $c to be 6 which is the missing element in $b array, priority should be only given to the array contents of $a.

Can anyone please help me out with this?
Thanks!

Community
  • 1
  • 1
PowerShell
  • 1,911
  • 8
  • 32
  • 54

2 Answers2

5

Either empo's approach, or

$a1=@(1,2,3,4,5,8)
$b1=@(1,2,3,4,5,6)
Compare-Object $a1 $b1 | 
   Where-Object { $_.SideIndicator -eq '<=' } | 
   Foreach-Object { $_.InputObject }

returns 8

stej
  • 27,607
  • 11
  • 68
  • 102
3
$c = $a | ? {!($b -contains $_)}

The priority will be given to the variable you "pipe".

Emiliano Poggi
  • 23,732
  • 7
  • 51
  • 65