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,991
  • 8
  • 35
  • 57

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
  • 28,745
  • 11
  • 71
  • 104
4
$c = $a | ? {!($b -contains $_)}

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

Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67