Compare two arrays and save value that is repeated in another array

3

I am trying to compare 2 arrays ( a and b respectively) and the value of a that is repeated in b that forms another vector.

I have put it this way: when I want to compare the values entered in b with a I get an error and I can not understand how to solve it.

int[] a = new int[2];
int[] b = new int[2];
int[] c = new int[2];
int n = 0, m = 0, i, k, l=0;

for (i = 0; i < a.Length; i++)
{
    Console.WriteLine("Ingrese numero vector A");
    a[i] = Convert.ToInt32(Console.ReadLine());


}

for (k= 0; k < b.Length; k++)
{
    Console.WriteLine("Ingrese numero vector B");
    b[k] = Convert.ToInt32(Console.ReadLine());
    //Comparo el valor de b que estoy guardando con la posicion de a. si es correcto se guarda en el vector c
    if (b[k] == a[i])
        {

            b[k] = c[l];

            Console.WriteLine("C: " + c[l]);
        }

}
    
asked by Agus Veneziano 20.06.2018 в 06:00
source

3 answers

2

YOU HAVE 2 ERRORS:

  • if (b [k] == a [i])
  • b [k] = c [l]
  • The first error is in the 2nd for(k= 0; k < b.Length; k++){...} here you are doing the following check if (b[k] == a[i]) and you fail.

    The problem is where b [0] and b [1] will have the values set by the user, a [i] does not exist since the i is always evaluated as a 2 and it leaves the array, the real values being a [0], a [1] this is for declaring the i as global and performing a last i ++ when exiting the first for () {}.

    loop

    The second error is when you print the result c [l], in your case l is always 0 since you do not change the value, and you do not assign any element c [l] = ..., these two errors cause that you can never show the results of the same elements found in both arrays.

    Solution replace the second for with:

    for (k= 0; k < b.Length; k++){
        Console.WriteLine("Ingrese numero vector B");
        b[k] = Convert.ToInt32(Console.ReadLine())
    
        //PARA CADA VALOR DE B vas a buscar si existe en a.
        for (i = 0; i < a.Length; i++)
            if (b[k] == a[i]){
                  c[l] = b[k];
                  Console.WriteLine("C: " + c[l]);
                  l++; 
             }
    
        }
    }
    
        
    answered by 20.06.2018 / 08:36
    source
    5

    To expand the answers given to you, C # and specifically Linq has the method Intersect that does exactly what you're looking for. I'll give you an example:

    int[] arrayA = new int[] { 3, 4,6, 7, 9 };
    int[] arrayB = new int[] { 1, 2,3, 7, 8 };
    
    var resArray = arrayA.Intersect(arrayB).ToArray();
    //en resArray nos devuelve {3,7}
    
        
    answered by 20.06.2018 в 09:12
    3

    I will explain what you are doing, first fill an array to, until all right, but then, while filling in the b, you are comparing that the number you just entered is equal to a [i], that is, to the last number of the array a, since i, you have it defined as a global variable above and at the end of the for loop, i will be equal to the length of array a, that is, it will always be 2, for example, you always compare the numbers that you just entered in b with the last of a. And then, if that condition is met, you put that value always in c [l], and if l equals 0 and never change it, you're always replacing the first value of the array c.

    I fix your code a bit.

        int[] a = new int[2];
        int[] b = new int[2];
        int[] c = new int[2];
        int n = 0, m = 0, i, k, l=0;
    
        for (i = 0; i < a.Length; i++)
        {
            Console.WriteLine("Ingrese numero vector A");
            a[i] = Convert.ToInt32(Console.ReadLine());
        }
    
        for (k= 0; k < b.Length; k++)
        {
            Console.WriteLine("Ingrese numero vector B");
            b[k] = Convert.ToInt32(Console.ReadLine());
    
            //Aqui esta el error, justo despues de guardar un numero b le 
            //comparas solo con el ultimo de a siempre
    
            //Es necesario hacer otro bucle for aqui que recorra a para comparar 
            //el nuevo numero de b con todos los de a
    
            //Reseteamos i = 0 y repetimos bucle para que recorra a
            for (i = 0; i < a.Length; i++)
            {
                //Ahora si estamos comparando el nuevo numero de b con 
                //todos los de a, ya que i ahora cambia gracias al bucle nuevo
                if (b[k] == a[i])
                {
                    //El segundo problema esta aqui, l es siempre 0, hay que 
                    //incrementar l cada vez que se guarda un nuevo valor
                    b[k] = c[l];
    
                    //Incremento en 1 el valor de l para que el proximo 
                    //valor se guarde en la siguiente posicion vacia del array
                    l++;
    
                    Console.WriteLine("C: " + c[l]);
                }
            }
        }
    
        
    answered by 20.06.2018 в 07:39