C # - How do I check if 2 arrays have one or more numbers in common?

1

int[] numeros = new int[] {text_box1, text_box2, text_box3, text_box4};
int[] pares = new int[] {02, 04, 06, 08, 10};
int[] impares =  new int[] {01, 03, 05, 07, 09};
    
asked by anonymous 29.09.2018 / 04:58

1 answer

3

Following strictly the arrays of your question you can use Intersect of Linq:

int[] numeros = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] pares = new int[] { 02, 04, 06, 08, 10 };
int[] impares = new int[] { 01, 03, 05, 07, 09 };

int[] inputsPares = numeros.Intersect(pares).ToArray();
int[] inputsImpares = numeros.Intersect(impares).ToArray();

int quantosPares = inputsPares.Length;
int quantosImpares = inputsImpares.Length;

For the problem in question, which is just checking whether it is even or odd, you can use the mod operator, so you do not need the arrays pares and impares .

int[] inputsPares = numeros.Where(x => x % 2 == 0).ToArray();
int[] inputsImpares = numeros.Where(x => x % 2 != 0).ToArray();
    
29.09.2018 / 06:28