1
votes

How do I put into the collection of ListBox those items that doesn't have a pair from each array?

For example:

first array = 100 500

second array = 100 200 300 400 500 600 700 800

Now, how do I show those non matched values (200,400,600,700,800) into ListBox?

4

4 Answers

4
votes

You can use LINQ and Except method:

int[] result = secondArray.Except(firstArray).ToArray();
yourListBox.DataSource = result;

Also if you want to include values in firstArray that are not in secondArray go with the following query:

var result = firstArray.Except(secondArray).Union(secondArray.Except(firstArray)).ToArray();
1
votes

HashSet<int> can easily do set operations like this. Go check out the docs on that class and I'm sure you'll have an answer. I believe you will be interested in the method SymmetricExceptWith

1
votes

Might not be the most efficient, but you can do

var firstArray = new int[2] {100,500};
var secondArray = new int[8] {100,200,300,400,500,600,700,800};

var x = secondArray.Except(firstArray);

foreach(var item in x)
    Console.WriteLine(item);
0
votes

This is more or less a duplicate of Get the symmetric difference from generic lists.

var differences = listA.Except(listB).Union(listB.Except(listA));