Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!