Home > OS >  Find repetition in Javascript 4 dimensional array
Find repetition in Javascript 4 dimensional array

Time:10-18

I need to get the total number of repetitions in this 4-dimensional array. Here is the code :

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   " similar items in this array.");
}
countDuplicate(operatingSystem);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

It seems have a problem. How can I do this in JS ? Thank you !

CodePudding user response:

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}`)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use recursion and a Set to check each item in the array and see if it's unique. If it's an array you use recursion, if not you add to the count if it isn't unique.

function countDuplicate(array, arg) {
  let count = 0;
  const uniqueItems = new Set()

  const checkArray = arr => {
      for (let item of arr) {
          if (Array.isArray(item)) {
              checkArray(item)
          } else if (uniqueItems.has(item)) {
              count  
          } else {
              uniqueItems.add(item)
          }
      }
  }
  
  checkArray(array)

  console.log("There is "   count   " similar items in this array.");
}

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

countDuplicate(operatingSystem)
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related