Home > Enterprise >  Comparing arrays of ints
Comparing arrays of ints

Time:01-05

I am working on the following problem: Write a function called same, which accepts two arrays. The function should return true if every value in the array has it's corresponding value squared in the second array. The frequency of the values must be the same.

The solution seems to be working except for the part where I compare the second array to the third array. Console log tells me they are the same but the comparison fails indicating they are not equal.

Why would second===third be false when they are the same?

function same(first, second) { 
    first.sort((a,b) => a-b)
    console.log(first)

    second.sort((a,b) => a-b)
    console.log(second)

    let third = first.map((a) => a**2)
    console.log(third)

    if (second === third) return true
    else return false
}
$ node same.js
[ 1, 2, 3 ]
[ 1, 4, 9 ]
[ 1, 4, 9 ]
false

CodePudding user response:

You're using strict equality (===). In Javascript, objects are compared by reference, not by value. So in your case you're getting false because you're comparing two different objects in memory, they just happen to hold the same values. Depending on the situation, you could either stringify two arrays and compare them (easiest, but not recommneded):

function same(first, second) { 
  first.sort((a,b) => a-b)
  second.sort((a,b) => a-b)

  let third = first.map((a) => a**2)

  return second.toString() === third.toString()
} 

Or, you could actually iterate through the arrays and compare each value (preferred solution):

function same(first, second) { 
  first.sort((a,b) => a-b)
  second.sort((a,b) => a-b)

  if (first.length !== second.length) return false

  for (let i = 0; i < first.length; i  ) {
    if (first[i] ** 2 !== second[i]) return false
  }

  return true
}

Or, using the every array method:

function same(first, second) { 
  return first
    .sort((a,b) => a-b)
    .map((a) => a**2)
    .every((value, index) => value === second.sort((a,b) => a-b)[index])
}

CodePudding user response:

In JavaScript comparing two objects/arrays is done by reference and not by value. Hence, Instead of comparing the values contained in the two arrays, JavaScript checks whether they point to the same reference. This means when comparing two arrays in JavaScript, using either the loose or tight equality operators (== or ===), It will always return false.

I think best approach is to compare the arrays by iterating. To achieve that we can either use for loop or Array#every method which tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Live Demo :

const equalsCheck = (arr1, arr2) =>
    arr1.length === arr2.length &&
    arr1.every((v, i) => v === arr2[i]);

const arr1 = [1, 4, 9];
const arr2 = [1, 4, 9];

console.log(equalsCheck(arr1, arr2));

CodePudding user response:

Arrays are compared item by item. Replace this:

if (second === third) return true
    else return false
    

with:

return second.every((v,i) => v === first[i])

like so:

function same(first, second) { 
    first.sort((a,b) => a-b)
    console.log(first)

    second.sort((a,b) => a-b)
    console.log(second)

    let third = first.map((a) => a**2)
    console.log(third)

    return second.every((v,i) => v === third[i]);
}

console.log( same([1,2,3], [1,4,9]) );

  • Related