0

My code is not returning the right count, is there a small error or do I have to scrap this strategy entirely?

function countIdenticalArrays(arr1, arr2, arr3, arr4) {
  var count = 0;
  var obj = {};
  var culmArr = [...arguments];
  for (var i = 0; i < culmArr.length; i++) {
    if (obj[culmArr[i]] === undefined) {
      obj[culmArr[i]] = 1;
    } else {
      obj[culmArr[i]]++;
    }
    if (obj[culmArr[i]] > 1) {
    count++ 
  }
  }
  return count;
}

console.log(countIdenticalArrays([0, 0, 0], [0, 1, 2], [0, 0, 0], [2, 1, 0])); // 2
console.log(countIdenticalArrays([0, 1, 0], [0, 1, 2], [0, 2, 0], [2, 1, 0])); // 0
console.log(countIdenticalArrays([0, 1, 2], [0, 1, 2], [0, 1, 2], [2, 1, 0])); // 3
Nick
  • 138,499
  • 22
  • 57
  • 95
  • 2
    What is your expected output for `countIdenticalArrays([0, 0, 0], [0, 1, 2], [0, 0, 0], [0, 1, 2]))`? – Nick Mar 29 '21 at 06:45
  • I commented the expected output, scroll a little to the right – juan batista Mar 29 '21 at 06:51
  • 1
    I can see that thanks, I am asking about a different input value than you have in your code, it has 2 array values that are both repeated twice – Nick Mar 29 '21 at 06:52

4 Answers4

1

Your problem is that you only start counting when you find the second occurrence of an array, so your count will always be one short. It's probably easier to loop over the entire array first and then select the highest value greater than 1:

function countIdenticalArrays(arr1, arr2, arr3, arr4) {
  var count = 0;
  var obj = {};
  var culmArr = [...arguments];
  for (var i = 0; i < culmArr.length; i++) {
    if (obj[culmArr[i]] === undefined) {
      obj[culmArr[i]] = 1;
    } else {
      obj[culmArr[i]]++;
    }
  }
  for (let c of Object.values(obj)) {
    if (c > 1 && c > count) count = c;
  }
  return count;
}

console.log(countIdenticalArrays([0, 0, 0], [0, 1, 2], [0, 0, 0], [2, 1, 0])); // 2
console.log(countIdenticalArrays([0, 1, 0], [0, 1, 2], [0, 2, 0], [2, 1, 0])); // 0
console.log(countIdenticalArrays([0, 1, 2], [0, 1, 2], [0, 1, 2], [2, 1, 0])); // 3
Nick
  • 138,499
  • 22
  • 57
  • 95
  • is the && c > count necessary? – juan batista Mar 29 '21 at 07:29
  • @Nick, when we console.log( countIdenticalArrays([0, 0, 0], [0, 1, 2], [0, 0, 0], [2, 1, 0], [0, 1, 2]) )); the output should be 4 but the above code gives 2. –  Mar 29 '21 at 07:29
  • @juanbatista yes, that ensures we get the maximum value from `obj`. – Nick Mar 29 '21 at 07:30
  • @Rōnin I asked that question of OP (see my first comment under the question) and haven't yet received an answer as to what the correct return value is (2 or 4). If it is 4, I will edit my answer appropriately but until then this code meets OPs requirements. – Nick Mar 29 '21 at 07:31
1

Your approach seems a bit overdone. Another way of doing it is by using a filter on the source array. Then loop this array and check if a same array exists.

You can check this by first ruling out it's not the same array. Then checking if it's the same length, and finally checking if the array has the same elements at the same position

In code it would look like this:

function countIdenticalArrays(...arrays) {
  return arrays.filter((arr1) => arrays.some((arr2) =>
    arr1 !== arr2 &&
    arr1.length === arr2.length &&
    arr1.every((item, idx) => arr2[idx] === item)
  )).length;
}

console.log(countIdenticalArrays([0, 0, 0], [0, 1, 2], [0, 0, 0], [2, 1, 0])); // 2
console.log(countIdenticalArrays([0, 1, 0], [0, 1, 2], [0, 2, 0], [2, 1, 0])); // 0
console.log(countIdenticalArrays([0, 1, 2], [0, 1, 2], [0, 1, 2], [2, 1, 0])); // 3

Another way would be to use reduce:

function countIdenticalArrays(...arrays) {
  return arrays.reduce((count, arr1) => count + arrays.some((arr2) =>
    arr1 !== arr2 &&
    arr1.length === arr2.length &&
    arr1.every((item, idx) => arr2[idx] === item) ? 1 : 0
  ), 0);
}

console.log(countIdenticalArrays([0, 0, 0], [0, 1, 2], [0, 0, 0], [2, 1, 0])); // 2
console.log(countIdenticalArrays([0, 1, 0], [0, 1, 2], [0, 2, 0], [2, 1, 0])); // 0
console.log(countIdenticalArrays([0, 1, 2], [0, 1, 2], [0, 1, 2], [2, 1, 0])); // 3
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
0

function countIdenticalArrays(arr1, arr2, arr3, arr4) {
  var count = 0;
  var obj = {};
  var culmArr = [...arguments];
  for (var i = 0; i < culmArr.length; i++) {
    if (obj[culmArr[i]] === undefined) {
      obj[culmArr[i]] = 1;
    } else {
      obj[culmArr[i]]++;
    }
    if (obj[culmArr[i]] > 1) {
    count++ // i think here is the problem
  }
  }
  return count;
}

console.log(countIdenticalArrays([0, 0, 0], [0, 1, 2], [0, 0, 0], [2, 1, 0])); // 2
console.log(countIdenticalArrays([0, 1, 0], [0, 1, 2], [0, 2, 0], [2, 1, 0])); // 0
console.log(countIdenticalArrays([0, 1, 2], [0, 1, 2], [0, 1, 2], [2, 1, 0])); // 3

i think you should sum all values of the obj array.
current code add only 1 to the count when it found a couple of same data.
my recommendation : count++ => count = obj.reduce(function(x,y){ return x+y; });
+ hmm reduce doesnt work. sorry. it seems need to find other way

0

You can compare array to array and add count indentical array into your array indentical count. You can use compare array with reading refference on the questions this link below:

How to compare arrays in JavaScript?

hery
  • 32
  • 1