How do I check if two words have a common char?
ex. : “word” and “letter” have common “r”
“word” and “email” haven’t any common chars
This code is wrong because if two words have 2 common chars I get 4 in the result
int numberOfCommonChars = (from c1 in word1.ToCharArray()
from c2 in word2.ToCharArray()
where c1 == c2
select c1).Count();
Your code isn’t working becasue using multiple
fromclauses creates a full outer joinYou need to use
Intersect:Although it doesn’t show in IntelliSense,
StringimplementsIEnumerable<char>, so you don’t need to callToCharArray().Note that this will only count each character once, so if both strings contain the same character twice, this will only count it once.
If you want to count multiple occurrences, use the following code: