c# - How to get the 1st match between 2 string arrays -
i have 2 string arrays. 1 base , other changing.
string[] basearray = { "gold", "silver", "bronze" }; string[] readarray = { "bronze", "silver", "gold" }; // after comparing readarray on basearray result should //string match = "gold";
i want 1st in order of basearray.
//example2 string[] readarray = { "bronze", "silver" }; //string match should "silver"
if want 1 result, using linq:
string firstmatch = basearray.firstordefault(readarray.contains);
if want 1 result, not using linq:
string firstmatch = null; foreach(string element in basearray) { if (array.indexof(readarray, element) >= 0) { firstmatch = element; break; } }
if want matching elements, using linq:
string[] common = basearray.intersect(readarray).toarray();
if want matching elements, not using linq:
hasset<string> common = new hashset<string>(readarray); result.intersect(basearray);
Comments
Post a Comment