Detect if in two different chains there is at least 1 equal character [closed]

2

I would like to know how to make a quick comparison between two chains to know if they share at least 1 character in common.

Example:

I have manzana and piña . In this case there is a coincidence. The a is present in both. Therefore, I want the code to recognize if there is at least 1 match between both chains.

About this code, I do not know how I would implement it:

public static void Main(string[] args)
{
    Console.WriteLine(ExisteCoincidencia("manzana", "piña")); //Debería ser True
    Console.WriteLine(ExisteCoincidencia("no", "piña"));      //Debería ser False
}

private static bool ExisteCoincidencia(string s1, string s2)
{
    // ¿de qué forma podría lograrlo?
}
    
asked by AlexH 22.03.2017 в 02:37
source

1 answer

3

A very simple and concise way to do it is to use Enumerable.Intersect to find the characters in common, and then combine the result with Enumerable .Any to return true if there was at least one match.

Example:

public static void Main(string[] args)
{
    Console.WriteLine(ExisteCoincidencia("manzana", "piña"));
    Console.WriteLine(ExisteCoincidencia("no", "piña"));
}

private static bool ExisteCoincidencia(string s1, string s2)
{
    return s1.Intersect(s2).Any();
}

Result:

  

True
  False

Demo

    
answered by 22.03.2017 / 02:56
source