3

I have problem with the if-else statement and also splice seems to be not working.

function same(arr1, arr2) {
    if (arr1.length !== arr2.length) {
        return false;
    }
    for (let i = 0; i < arr1.length; i++) {
        for (let j = 0; j < arr2.length; j++) {
            let correctIndex = arr1[i] * 2;
            if (correctIndex !== arr2[j]) {
                return false;
            }
        }
        console.log(arr2);
        arr2.splice(correctIndex, 1)
    }
    return true;
}

same([1, 2, 3, 2], [9, 1, 4, 4]);
H_B
  • 33
  • 2
  • Sounds like you only want to `return false` once *all* elements in the `arr2` are not the desired one? – Bergi Jul 18 '19 at 21:16
  • You really should rename `correctIndex`, it's not an index. And if you want to get the square, then you should not do `* 2` but `** 2` (or `* arr1[i]`). – Bergi Jul 18 '19 at 21:17
  • I would just sort them and square the one and compare each index.. – epascarello Jul 18 '19 at 21:20
  • Problem with your logic is you are saying if it does not match to exit out, problem is you are not checking every index before you exit out. – epascarello Jul 18 '19 at 21:21

5 Answers5

2

try this:

const sortNumber = numArray => numArray.sort((a, b) => a - b);

const same = (_arr1, _arr2) => { 
    const arr2 = sortNumber(_arr2);
    return sortNumber(_arr1).every((res,i)=>arr2[i]===res**2);
}

console.log(same([1, 2, 3, 2], [9, 1, 4, 4]))
Ghoul Ahmed
  • 4,446
  • 1
  • 14
  • 23
0

You need to loop through the first array, referencing the corresponding index in the second array. You can return false as soon as you find an entry that is not a square.

function same(arr1, arr2){
  if(arr1.length !== arr2.length) return false;

  arr1.sort();
  arr2.sort();

  // visit each item in arr1 exactly once
  for(let i=0; i<arr1.length; i++){
    // if entry at arr2 isn't the square of that entry in arr1, return false immediately
    if(arr1[i] * arr1[i] !== arr2[i]) return false;
  }

  // we've checked each item, return true
  return true;
}

There's no need to use splice() here since you only need to return true or false, not modify either array. Also, you don't need to use nested for loops, since you can access each array directly.

0

You can also check using the method of frequency counter in O(n) complexity.

function same(arr1, arr2) {
  if (arr1.length !== arr2.length) {
    return false;
  }

  let fc1 = {};
  let fc2 = {};

  for (let val of arr1) {
    fc1[val] = (fc1[val] || 0) + 1;
  }
  
  for (let val of arr2) {
    fc2[val] = (fc2[val] || 0) + 1;
  }

  for (let key in fc1) {
    if (!(key ** 2 in fc2)) {
      return false;
    }

    if (fc1[key] !== fc2[key ** 2]) {
      return false;
    }
  }
  
  return true;
}
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
0

function areSquaredValues(arr1, arr2) {
  if (arr1.length !== arr2.length) {
    return false;
  }
  
  const obj = {};
  
  for (let num of arr1) {
    obj[num * num] = obj[num * num] ? obj[num * num] + 1 : 1;
  }
  
  for (let num of arr2) {
    if (!obj[num] || obj[num] === 0) {
      return false;
    } else {
      obj[num] -= 1;
    }
  }
  
  return true;
}

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

The function works as follows:

First, it checks if the length of both arrays is the same. If not, it immediately returns false. It then creates an empty object obj to store the square values of the first array. The function then iterates through the first array and adds each square value as a key in obj, with the value being the number of times the square value appears in the array. This is done by checking if the key already exists in the object and incrementing its value if it does, or setting it to 1 if it doesn't. Next, the function iterates through the second array and checks if each value is a key in obj. If it isn't, or if its value is 0 (which means it has already been matched with a value in the second array), the function immediately returns false. Otherwise, it decrements the value of the corresponding key in obj. Finally, if the function has made it through both iterations without returning false, it returns true because all values in the second array correspond to the square values of the first array.

Kkkk Kkkk
  • 261
  • 4
  • 4
-1

You don't need nested loops for this. Here's a one-pass algorithm with a single for loop

function same(arrOne, arrTwo) {
  if (arrOne.length === arrTwo.length) {
    let totalOne = 0;
    let totalTwo = 0;
    for (let x = 0; x < arrOne.length; x++) {
      totalOne += arrOne[x] ** 2;
      totalTwo += arrTwo[x];
    }
    if (totalOne === totalTwo) {
      return true;
    } else return false;
  }
  return false;
}

console.log(same([-1, -2, -3], [4, 1, 9]));
console.log(same([1, 2, 3], [1, 9]));
console.log(same([4, 5, 6], [16, 25, 36]));
console.log(same([1, 2, 3, 2], [9, 1, 4, 4]));

This handles even the case where the items in the second array are not in order.

Naser Mohd Baig
  • 155
  • 1
  • 13