1

Here is the question

Q) take 2 arrays and pass it to a function, then check if the square of the array1 is contained in the array2 (Note: Order doesn't matter, also 2 duplicates in arr1[1,2,2], there should be 2 duplicates in arr2[1,4,4]).

I have this code in which, i am trying to convert the indexOf to a for loop and i have included that code which i tried after this code

function same2(arr1, arr2){
  if (arr1.length !== arr2.length){
    return false;
  }
  for (let i = 0; i < arr1.length; i++){
    let currentIndex = arr2.indexOf(arr1[i] ** 2);
    // if the square of arr1 is contained in any of the index in arr2
      if (currentIndex === -1){
        return false;
      }
        console.log(arr2);
        arr2.splice(currentIndex, 1);
    }
  return true;
  }


same2([10,2, 3, 5], [100, 4, 25, 9]);

Here is the code with 2 for loop and its giving wrong output for the corresponding input.

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 currentIndex = arr1[i] ** 2;
            console.log(currentIndex);
            if (currentIndex === -1){
                return false;
            }
            if (arr2[j] === currentIndex){
                arr2.splice(currentIndex, 1);
//                 console.log(arr2);
            } 

        }
    }
    return true; 
}
same([1, 10,2, 4], [10, 1, 16, 4]);

I know i have problem with the index of the array, but i am not able to break it.

Maestro
  • 43
  • 9
  • About the question, do you mean you want to find a single number which is a square of a single number of the other array? (Or multiple numbers etc) – Dennis Vash Oct 05 '19 at 11:35
  • Also, is it a homework question? Do you have any restrains (must use a loop etc)? – Dennis Vash Oct 05 '19 at 11:39
  • More precisely: do you need to check if the square of all numbers in input1 is contained in input2? What about duplicates? What about extra elements in input2? – ZorgoZ Oct 05 '19 at 11:40
  • `currentIndex` is not an index, hence the name of the variable is wrong. Why do you calculate `currentIndex` for every round of the inner loop if its value will only change per round of the outer loop? `currentIndex` won't ever be negative hence it won't ever be `-1`. – Andreas Oct 05 '19 at 11:40
  • @DennisVash no, i want to check if each of the array element in arr2 has a corresponding element in arr1 which is its sq. – Maestro Oct 05 '19 at 11:40
  • its not a homework question and its just from a course (ds algo) that i am having. – Maestro Oct 05 '19 at 11:42
  • Just remove the line with `splice`, it is making the inner `for` loop skip elements. Or if it is really needed to remove it, loop backwards. See referenced Q&A – trincot Oct 05 '19 at 11:43
  • the length of both the arrays must be equal , duplicates are fine its should return true, if there are corresponding sq for arr1 in arr2. – Maestro Oct 05 '19 at 11:44
  • @trincot even if i remove splice in program with 2 for loops its still returning true for the test case with ```([1, 10,2, 4], [10, 1, 16, 4]);``` as clearly there is not sq for 10 in arr2 – Maestro Oct 05 '19 at 11:49
  • Well, yes, that is because `currentindex` is not an index, but the square. You mixed up quite a few things compared to the first code, that was correct. Just look how a square can never be -1. Instead use a boolean variable to indicate whether there was a match (and exit it when there was one), and after that innermost loop return `false` if that variable is not set (to true). https://jsfiddle.net/xjfps82h/ – trincot Oct 05 '19 at 12:19
  • @trincot yeah dude this one does it, i will try to understand it properly [link](https://jsfiddle.net/xjfps82h/) – Maestro Oct 05 '19 at 12:34

1 Answers1

1

What about this functional approach?

test2 = (i1, i2) => i1.length === i2.length && i1.reduce((a,c) => a && i2.includes(c*c), 1)

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

Update 1 Here is a double loop approach, but somehow different from yours as you have several things wrong there. First of all: with splice you are modifying the array for which you have a for loop - that is something you should never do. Secondly, you have recalculated the value you are looking for in the inner loop - that made it absolutelly unusable. You don't need that variable at all, but I have used it to be more straightforward.

Update 2 If you want to have an element in arr2 to count only once, you can introduce the trick to remove that element - without modifying the array with splice.

function same(arr1, arr2){
    if (arr1.length !== arr2.length){
        return false;
    }
    for (let i = 0; i < arr1.length; i++){
        let lookFor = arr1[i] ** 2;
        let found = false; 
        for (let j = 0; j < arr2.length; j++){
            if (arr2[j] === lookFor){
              delete arr2[j]
              found = true
              break;
            }
        }
        if(!found) return false
    }
    return true; 
}

console.log(same([1,2,2],[1,4,4]))
console.log(same([1,2,2],[1,1,4]))
ZorgoZ
  • 2,974
  • 1
  • 12
  • 34
  • i know there are different approaches, but i am having difficulties with 2 for loops. – Maestro Oct 05 '19 at 11:51
  • @Maestro see update – ZorgoZ Oct 05 '19 at 12:02
  • check for this case ```console.log(same([1,2,2],[1,1,4]))``` its still returning true as it have returned false. – Maestro Oct 05 '19 at 12:27
  • @Maestro of course it does. You wrote yourself: _the length of both the arrays must be equal , duplicates are fine its should return true, if there are corresponding sq for arr1 in arr2_. This the above sample input satisfies this constraint. Of course I understand what you mean now, but you never stated that before. – ZorgoZ Oct 05 '19 at 12:34
  • @Maestro see update2 – ZorgoZ Oct 05 '19 at 12:38
  • yep, i understood your approach its correct, yeah i should have mentioned that for 2 duplicates in arr1[1,2,2], there should be 2 duplicates in arr2[1,4,4]. Thanks this is the proper answer i was looking for hope u have a great day :). – Maestro Oct 05 '19 at 12:39
  • in your program, when there is `if (!found) {return false; }` when this executes the whole whole program will terminate and will just return false? or the for loop with ith iteration will execute for the rest of the indexes? – Maestro Oct 06 '19 at 04:17
  • It will exit the function with false result. It will not try other indexes, as if one is missing no need to test anything else at all. – ZorgoZ Oct 06 '19 at 08:33