Catch only equal elements in an array

0

I have an array for example:

1 4 7
1 3 4 6 7
1 2 3 4 5 6

I have to see the numbers that repeat on ALL lines, in the example, should be the numbers: 1 and 4. For they are repeating on all lines.

Vector<Integer> validos = new Vector<Integer>();
int cont = 0;
for (int linha = 0; linha < resultado.length; linha++)
{

    for (int coluna = 0; coluna < resultado.length - 1; coluna++)
    {
        for (int linha2 = (linha + 1); linha2 < resultado.length; linha2++)
        {
            for (int coluna2 = 0; coluna2 < resultado.length - 1; coluna2++)
            {
                if(resultado[linha][coluna] == resultado[linha2][coluna2])
                {
                    cont ++;
                }
            }
        }
    }
}

Where the array is the variable resultado , which is a int[][] , I tried to implement a cont that if there were the number on all rows it would then count the number of rows in the array, eg 3.

Only the code is not working.

The goal is to get these repeated numbers and put them in Vector validos

    
asked by anonymous 17.06.2016 / 17:23

1 answer

1

I'm not sure what language you wrote. Anyway I wrote in C #, you will know how to make the adaptations with class names List , Vector , etc.

At the end of the code, the variable int[] validos will have only 1, 2, 3 . As you want, but I had to add 3 in the first level of the array because it did not exist.

        int[][] vetor = new int[][] {
            new int[] { 1, 3, 4, 7 },
            new int[] { 1, 3, 4, 6, 7 },
            new int[] { 1, 2, 3, 4, 5, 6 }
        };

        List<int> validosList = new List<int>();

        for (int i = 0; i < vetor.Length; i++)
        {
            for (int j = 0; j < vetor[i].Length; j++)
            {
                int numero = vetor[i][j];

                if (validosList.Contains(numero))
                {
                    continue; //Número já validado. Pula essa verificação.
                }

                int contagem = 0;

                for (int k = 0; k < vetor.Length; k++)
                {
                    for (int l = 0; l < vetor[k].Length; l++)
                    {
                        int numeroEmVerificacao = vetor[k][l];

                        if (numero == numeroEmVerificacao)
                        {
                            contagem++;
                            break; //Vai pra próxima linha do array
                        }
                    }                        
                }

                if (contagem == vetor.Length)
                {
                    validosList.Add(numero);
                }

            }
        }

        int[] validos = validosList.ToArray();
    
17.06.2016 / 18:00