Take only equal elements in a matrix

Asked

Viewed 543 times

0

I have a matrix for example:

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

I have to see the repeating numbers in ALL lines, in the example, the numbers should be the result: 1 and 4. For it is they who repeat in 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 matrix is the variable resultado, which is a int[][], tried to implement a cont that if there were the number on all rows it would then be worth the number of rows in the matrix, example, 3.

Only the code isn’t working.

The goal is to take these repeated numbers and put in the Vector validos

  • Three does not appear in the first line. : p

  • Typing error kk is 1 and 4 in case

  • Renan certainty? Where does it? Because it is strange, even using Java8, Eclipse does not show it as deprecated, as some Time methods, etc.

  • @lvcs I have no way to post links here, I’m by mobile. But in Google you find much on the subject.

1 answer

1


I don’t know which language you wrote. Anyway I wrote in C#, you will know how to make the adaptations with the class names List, Vector, etc..

At the end of the code, in the variable int[] validos will only have 1, 2, 3. How do you want, but I had to add 3 on the first level of the matrix because it didn’t 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();
  • 1

    The language was in the tag, it’s java.

  • 1

    I adapted it to Java, it worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.