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;
   }
}
4
  • 1
    Your code does not compile: array initialization and i is used twice Commented Apr 4, 2018 at 4:26
  • yeah, i just notice it. Sorry about it. updated the question. Commented Apr 4, 2018 at 6:20
  • Possible duplicate of C# comparing two string arrays Commented Apr 4, 2018 at 6: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. Commented Apr 4, 2018 at 7:39

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I've found your method is faster, too :)
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();

7 Comments

Nice to see that you have done the comparison. I will delete my answer as it is similar to what you have done.
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 Apologies, I'll include my benchmark data set.
@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<string>(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.
@john apples to oranges:) Your test compared ordinal vs. ordinal-ignore-case... If you use same comparison for both your version is faster.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.