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

149
Visualizações
Find repetition in Javascript 4 dimensional array

Sorry I misdescribed my problem :

If the argument value is "macOS" in the call like this -> countDuplicate(operatingSystem, "macOS");

The function must return the number of 9. The same for other values(Windows, Unix...).

Thank you !

let operatingSystem = [
  ["macOS", "Windows", "Unix"],
  ["macOS", ["Windows", "Unix", "macOS"], "Unix"],
  [["macOS", "Windows", "Unix"], "Windows", "Unix"],
  ["Unix", "macOS", ["Windows", "Unix", "macOS"]],
  [["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
  [["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
];

function countDuplicate(array, arg) {
  let count = 0;

  for (let i = 0; i < operatingSystem.length; i++) {
    for (let j = 0; j < operatingSystem[i].length; j++) {
      for (let k = 0; k < operatingSystem[i][j].length; k++) {
        for (let l = 0; l < operatingSystem[i][j][k].length; l++) {
          let str = operatingSystem[i][j][k][l];
          if (str.indexOf(arg) > -1) {
            count += 1;
            break;
          }
        }
      }
    }
  }
  console.log("There is " + count + " of " + arg + " similar items in this array.");
}
countDuplicate(operatingSystem, "macOS");

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

0

First you flat() the multi-dimensional array and then use this solution: https://stackoverflow.com/a/19395302/8205497

let operatingSystem = [
  ["macOS", "Windows", "Unix"],
  ["macOS", ["Windows", "Unix", "macOS"], "Unix"],
  [["macOS", "Windows", "Unix"], "Windows", "Unix"],
  ["Unix", "macOS", ["Windows", "Unix", "macOS"]],
  [["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
  [["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"]
];

let counts = {};

operatingSystem.flat(4).forEach(function (x) { counts[x] = (counts[x] || 0) + 1; });

console.log(counts);

let sum = 0;
Object.values(counts).forEach(x => sum += x)

console.log(`The total sum is: ${sum}`)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use recursion to count all elements. You can also do a flat array.

In recursion, If the element is array iterate and calls for each value else add to map.

let operatingSystem = [
  ["macOS", "Windows", "Unix"],
  ["macOS", ["Windows", "Unix", "macOS"], "Unix"],
  [["macOS", "Windows", "Unix"], "Windows", "Unix"],
  ["Unix", "macOS", ["Windows", "Unix", "macOS"]],
  [["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
  [["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"],
];

function countDuplicate(array, res = {}) {
  if (typeof array === "object") {
    array.forEach((x) => countDuplicate(x, res));
  } else {
    if (!res[array]) res[array] = 0;
    res[array]++;
  }
}
let res = {};
countDuplicate(operatingSystem, res);

console.log(res);

console.log(Object.entries(res));

about 4 years ago · Juan Pablo Isaza Relatório

0

This is a straightforward recursion. We can start with a list of values and a target, then scan through the values, adding to our running counter for each. If the value is an array, we recur on it with the same target. Otherwise if it matches the target, we add 1, and add nothing otherwise. It might look like this:

const countDuplicate = (xs, t) => 
  xs .reduce ((c, x) => c + (Array .isArray (x) ? countDuplicate (x, t) : x == t ? 1 : 0), 0)

const operatingSystem = [["macOS", "Windows", "Unix"], ["macOS", ["Windows", "Unix", "macOS"], "Unix"], [["macOS", "Windows", "Unix"], "Windows", "Unix"], ["Unix", "macOS", ["Windows", "Unix", "macOS"]], [["macOS", "Windows", ["Unix", "Windows", "macOS"]], "Windows", "Unix"], [["Linux", "Android", ["Unix", "Windows", "macOS"]], "Windows", "Unix"]];

['macOS', 'Windows', 'Unix', 'Linux', 'Android', 'Other'] .forEach (
  os => console .log (`${os}: ${countDuplicate (operatingSystem, os)}`)
)

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