H
H
Heppart2020-09-25 12:13:54
Arrays
Heppart, 2020-09-25 12:13:54

C#. There are two arrays, how to check the data for a match?

Hello!

There is an array containing a series of data, for example:

string[] array = { "Сайт №1", "Сайт №2", "Сайт №3" };

and 2nd array containing more data
string[] array = { "Сайт №1", "Сайт №2", "Сайт №3", "Сайт №4" };

How to check if the data of the first array matches the data of the second array?
Thank you!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vladimir Korotenko, 2020-09-25
@firedragon

I don't understand you, but hold on, something is right for you

array2.Except(array1)
array1.Except(array2)
array1.Intersect(array2) // видимо это

W
Wataru, 2020-09-25
@wataru

The option is stupid - for each element of the first array, loop through the second array and compare.
A smarter option: Sort both arrays, walk through them with two pointers in parallel (If the current elements match, we find a match and move both pointers, otherwise we move the pointer to a smaller element. If one of the arrays is over, that's it).
Best option: Iterate over a smaller array and add its elements into a HashSet<> (via Add()). Then go through the second array and check each element through the Contains() method of the set.

A
Alexander, 2020-09-25
@avorsa

bool isEqual = Enumerable.SequenceEqual(array1, array2);

just to compare

Y
yuopi, 2020-09-25
@yuopi

string[] arr1 = {"a", "bb", "ccc", "zzzz"};
string[] arr2 = {"a", "b", "ccc", "dddd", "ee"};

var result = arr1.Join(arr2, s1 => s1, s2 => s2, (s1, s2) => s1);

D
Dmitry Pavlov, 2020-09-25
@Stalker31

int []mas1;
int[]mas2;
for(int i=0;i<mas1.Lenght;i++){
for(int i2=0;i2<mas2.Lenght;i2++){
if(mas1[i]==mas2[i2]){
//Если данные совпали
}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question