4

What is the easiest way to check if a string exist in 2 array? p/s Is there is LINQ method to replace this?

// Old school method
bool result = false;
var stringArray1 = new string[] { "ABC", "EFG", "HIJ" };
var stringArray2 = new string[] {"123", "456", "ABC"};
for (var i = 0; i < stringArray1.Count; i++) {
    var value1 = stringArray1[i];
   for (var j = 0; j < stringArray2.Count; j++) {
       var value2 = stringArray2[j];
       if(value1 == value2)
           result = true;
   }
}
Eugene Lim
  • 269
  • 3
  • 15
  • 1
    Your code does not compile: array initialization and `i` is used twice – Backs Apr 04 '18 at 04:26
  • yeah, i just notice it. Sorry about it. updated the question. – Eugene Lim Apr 04 '18 at 06:20
  • Possible duplicate of [C# comparing two string arrays](https://stackoverflow.com/questions/17212254/c-sharp-comparing-two-string-arrays) – daniu Apr 04 '18 at 06:55
  • @daniu, The other question is asking to compare complete string array down to its same order and size. I'm only asking if there is any strings that exist within the 2 arrays. Similar, but different. Thanks for searching, its a good reference too. – Eugene Lim Apr 04 '18 at 07:39

2 Answers2

7

For a case sensitive search, you can just do this

var result = stringArray1.Any(x => stringArray2.Contains(x));

As answered Intersect does the the job very well too.

Though if you want a more robust culturally insensitive version

You could use

var culture = new CultureInfo("en-US");
var result =  stringArray1.Any(x => 
                  stringArray2.Any(y => 
                      culture.CompareInfo.IndexOf(x, y, CompareOptions.IgnoreCase) >= 0));

Where culture is the instance of CultureInfo describing the language that the text is written in

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
6

You could intersect the two arrays and then check if there any items in the result:

var stringArray1 = new string[] { "ABC", "EFG", "HIJ" };
var stringArray2 = new string[] { "123", "456", "ABC" };
var result = stringArray1.Intersect(stringArray2).Any();

If you care case sensitivity, you can pass a StringComparer as the second argument of Intersect. For example:

var result = stringArray1.Intersect(stringArray2, StringComparer.OrdinalIgnoreCase).Any();
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    Nice to see that you have done the comparison. I will delete my answer as it is similar to what you have done. – harmands Apr 04 '18 at 04:40
  • 1
    I strongly suspect you used specially tuned data set to prove that O(n^m) code with 2 `Any` calls is faster than O(n+m) `.Intersect`... just saying – Alexei Levenkov Apr 04 '18 at 04:46
  • @Alexei Apologies, I'll include my benchmark data set. – ProgrammingLlama Apr 04 '18 at 04:54
  • @john I can't replicate your results (even on that dataset that cuts iterations earlier on "l")... Proper solution would be just convert one to `HashSet` and do `var d = new HashSet(stringArray2); var result = stringArray1.Any(s => d.Contains(s));` instead to remove `*m` in time complexity... Also indeed even O(m*n) solution is fine, especially if arrays are normally close enough so cost of constructing hashsets is not justified as basic iteration ends early enough. – Alexei Levenkov Apr 04 '18 at 05:33
  • @Alexei Is my [benchmark method](https://pastebin.com/A9vZZy1H) invalid? I'll remove any mention of benchmarks from my answer in case it isn't, I guess. – ProgrammingLlama Apr 04 '18 at 05:41
  • 1
    @john apples to oranges:) Your test compared ordinal vs. ordinal-ignore-case... If you use same comparison for both your version is faster. – Alexei Levenkov Apr 04 '18 at 14:18
  • @Alexei Doh! I copied the wrong one :) I feel like an idiot now! – ProgrammingLlama Apr 04 '18 at 14:21