0

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");

YvesK
  • 13
  • 2
  • Yes, you can do it with JavaScript. I recommend a recursive function. – jabaa Oct 17 '21 at 18:43
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 17 '21 at 18:45

3 Answers3

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}`)
Jason Landbridge
  • 968
  • 12
  • 17
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));
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
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)}`)
)
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103