I have 2 string arrays. The one is the base and the other is changing.
string[] baseArray = { "Gold", "Silver", "Bronze" };
string[] readArray = { "Bronze", "Silver", "Gold" };
// After comparing the readArray over the baseArray the result should be this
//string match = "Gold";
I want to get the 1st in order of the baseArray.
//Example2
string[] readArray = { "Bronze", "Silver" };
//string match should be "Silver"
If you only want one result, using LINQ:
string firstMatch = baseArray.FirstOrDefault(readArray.Contains);
If you only want one result, not using LINQ:
string firstMatch = null;
foreach(string element in baseArray)
{
if (Array.IndexOf(readArray, element) >= 0)
{
firstMatch = element;
break;
}
}
If you want all matching elements, using LINQ:
string[] common = baseArray.Intersect(readArray).ToArray();
If you want all matching elements, not using LINQ:
HasSet<string> common = new HashSet<string>(readArray);
result.Intersect(baseArray);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With