Getting the diff between two arrays in C#?
If you've got LINQ available to you, you can use Except
and Distinct
. The sets you asked for in the question are respectively:
- array2.Except(array1)
- array1.Except(array2)
- array1.Intersect(array2)
How do I get the differences between two string arrays in C#?
I think this the shortest way to solve this
foreach (string com in com2 )
{
if (!com1.Contains(com))
{
MessageBox.Show(com);
}
}
comparing two int arrays and printing out the difference
You can try to use linq Except
to get different from two arrays, then use Union
combine two arrays.
var result1 = arr2.Except(arr1);
var result2 = arr1.Except(arr2);
var result = result1.Union(result2);
c# online
Compare Two Arrays Of Different Lengths and Show Differences
You can use Except and Intersect ...
var Foo_Old = new[] { "test1", "test2", "test3" };
var Foo_New = new[] { "test1", "test2", "test4", "test5" };
var diff = Foo_New.Except( Foo_Old );
var inter = Foo_New.Intersect( Foo_Old );
var rem = Foo_Old.Except(Foo_New);
foreach (var s in diff)
{
Console.WriteLine("Added " + s);
}
foreach (var s in inter)
{
Console.WriteLine("Same " + s);
}
foreach (var s in rem)
{
Console.WriteLine("Removed " + s);
}
A better way to find a difference between two arrays
You're looking for the symmetric-difference, an operator that LINQ to Objects doesn't have yet (as of .NET 4.0). The way you've done it is perfectly fine - although you might want to consider extracting that bit out into a method of its own.
However, a more efficient way of accomplishing this would be with the HashSet<T>.SymmetricExceptWith
method.
var result = new HashSet<int>(a);
result.SymmetricExceptWith(b);
foreach (var d in result)
Console.WriteLine(d); // 1 4
Find Difference between 2 large arrays
You can compare each element between the two arrays. If there is a match add a 0
to array3
and look at the next element in both arrays. If there is no match, add a 1
to array3
and look at the next element in array2
. If array1
has no more elements then keep adding 1
until array2
has no more elements.
int[] array1 = {1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 0, 0, 0};
int[] array2 = {1, 1, 1, 2, 7, 7, 2, 2, 2, 2, 1, 2, 3, 2, 2, 3, 3, 4, 7, 2, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8, 4, 1, 1, 7, 7, 8, 8, 9, 9, 0, 0};
int index1 = 0;
int index2 = 0;
int[] array3 = new int[array2.Length];
while (index2 < array2.Length)
{
if (index1 >= array1.Length)
{
array3[index2] = 1;
index2 += 1;
}
else if (array1[index1] == array2[index2])
{
array3[index2] = 0;
index1 += 1;
index2 += 1;
}
else
{
array3[index2] = 1;
index2 += 1;
}
}
foreach (int i in array3)
{
Console.Write(i.ToString() + " ");
}
Output:
0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
Easiest way to compare arrays in C#
You could use Enumerable.SequenceEqual
. This works for any IEnumerable<T>
, not just arrays.
How to find the difference between two 2D arrays in c#
There is no need to copy data to 1D arrays, just write a double loop that iterates over both dimensions. For example assuming you have two 2D arrays , a
and b
:
var l0 = Math.Min(a.GetLength(0), b.GetLength(0));
var l1 = Math.Min(a.GetLength(1), b.GetLength(1));
var diff = new bool[l0, l1];
for (int i0 = 0; i0 < l0; i0++)
{
for (int i1 = 0; i1 < l1; i1++)
{
if (!a[i0, i1].Equals(b[i0, i1]))
{
diff[i0, i1] = true;
}
}
}
This produces an bool-array with the only the overlapping parts of the two matrices, and mark if the values are different or not. It should be trivial to change it to return one or the other of the values if they are different.
Comparing two arrays in C#
Since this is Homework, I will give you a homework answer.
Sure, you could use LINQ and rely on SequenceEqual
, Intersect
, etc, but that is likely not the point of the exercise.
Given two arrays, you can iterate over the elements in an array using foreach
.
int[] someArray;
foreach(int number in someArray)
{
//number is the current item in the loop
}
So, if you have two arrays that are fairly small, you could loop over each number of the first array, then loop over the all the items in the second array and compare. Let's try that. First, we need to correct your array syntax. It should look something like this:
int[] a = new int[] {1, 2, 3, 4};
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
Note the use of the curly braces {
. You were using the syntax to create a N-dimensional array.
bool hasDuplicate = false;
int[] a = new int[] { 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 7, 8 };
foreach (var numberA in a)
{
foreach (var numberB in b)
{
//Something goes here
}
}
This gets us pretty close. I'd encourage you to try it on your own from here. If you still need help, keep reading.
OK, so we basically need to just check if the numbers are the same. If they are, set hasDuplicate
to true.
bool hasDuplicate = false;
int[] a = new int[] { 8, 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 7, 8 };
foreach (var numberA in a)
{
foreach (var numberB in b)
{
if (numberA == numberB)
{
hasDuplicate = true;
}
}
}
This is a very "brute" force approach. The complexity of the loop is O(n2), but that may not matter in your case. The other answers using LINQ are certainly more efficient, and if efficiency is important, you could consider those. Another option is to "stop" the loops using break
if hasDuplicate
is true, or place this code in a method and use return
to exit the method.
Compare Two Arrays And Delete Same (Intersect) Value From Second Array
Just use, Where
, Contains
, in a Linq
statement then ToArray
In simple terms,
It filters array
two
by checking if arrayone
does not contain each element of twoConverts the output back to an array
Assigns its back to your variable
two
Example
string[] one={"my", "5", "two", "array", "hey?", "good", "day"};
string[] two = { "hello!", "how", "good", "day", "us", "very", "two", "hard", "learn", "it" };
two = two.Where(x => !one.Contains(x)).ToArray();
Console.WriteLine(string.Join(",", two));
Note, This is case sensitive
Output
hello!,how,us,very,hard,learn,it
Or a more performant way is to use Except
, which i totally forgot about (thanks to comments)
two = two.Except(one).ToArray();
Enumerable.Except Method
Produces the set difference of two sequences.
Enumerable.Where Method
Filters a sequence of values based on a predicate.
Enumerable.Contains Method
Determines whether a sequence contains a specified element.
Enumerable.ToArray(IEnumerable) Method
Creates an array from a IEnumerable.
Related Topics
Identityserver4 Register Userservice and Get Users from Database in ASP.NET Core
Random Number Between 2 Double Numbers
Converting a Generic List to a CSV String
Mvvm Light 5.0: How to Use the Navigation Service
Linq Where List Contains Any in List
Wpf - How to Create Menu and Submenus Using Binding
How to Delete a File Which Is Locked by Another Process in C#
Lambda Expression Using Foreach Clause
C#: Should Object Variables Be Assigned to Null
Make ASP.NET Wcf Convert Dictionary to JSON, Omitting "Key" & "Value" Tags
How to Wait for Async Method to Complete
Two-Way/Bidirectional Dictionary in C#
Linq Expression to Return Property Value
How to Read a Text File Without Locking It
How to Make a Call to My Wcf Service Asynchronous