Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

397
Visualizações
What is the best way to check multidimensional array in typescript or javascript to detect duplicate values?

I have two dimensional array like below:

  array =  [ [ 1, 1 ], [ 1, 2 ], [ 1, 1 ], [ 2, 3 ] ]

I want to compare value in array index to see if they have duplicate values. For example

 array[0] = [1,1];
 array[1] = [1,2];
 array[2] = [1,1];

We can see that value at index 0 and 2 are same that is [1,1]. So, in that case I want to have true flag. What is the most efficient way to do it? or What are different ways to do it? Any kind of suggestion or help would be great with bit of explanation. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can achieve it by convert the inner array elements into a string just for the comparison purpose.

Demo :

const arr = [[ 1, 1 ], [ 1, 2 ], [ 1, 1 ], [ 2, 3 ]];

const stringConversion = arr.map((item) => JSON.stringify(item))

const duplicateElements = stringConversion.filter((item, index) => stringConversion.indexOf(item) !== index)

console.log(duplicateElements.length ? true : false);

about 4 years ago · Juan Pablo Isaza Relatório

0

So, what I think you can do is:

  1. Cycle every element of the multidimensional array.
  2. Then check if the two element are the same by accessing the "row" with the index 0 and index 1

I think you can use different way to loop the array, this is what I tried:

// DECLARATIONS
array =  [[ 1, 1 ], [ 1, 2 ], [ 1, 1 ], [ 2, 3 ]];

// LOOPING THE ARRAY 
for (row of array)
{
  // RETURN TO CONSOLE OR WHATEVER THE BOOLEAN VALUE
  console.log(row[0] == row[1]);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

String to convert array to a one-dimensional array of strings and some to check if any element is duplicated:

const arr = [[1, 1], [1, 2], [1, 1], [2, 3]];

const result = arr.map(String).some((v, i, a) => a.indexOf(v) !== i);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda