Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda