-1

I have a JS logic that I am trying to implement, and now pondering why it is not working: I have two arrays with at least one similar element in them, but implementing the logic returned false - but I was expecting the opposite result. Please take a look:

 const array_one = [
  "Apparel",
  "Footwear",
];

const array_two = [
  "Soap",
  "Footwear",
];


const checkArray = (arr1, arr2) => {
    if (arr1.length !== arr2.length) {
      return false;
    } else {
      for (let i = 0; i < arr1.length; i++) {
        if (arr1[i] !== arr2[i]) {
          return false;
        }
      }
    }
    return true;
  };

  console.log(checkArray(array_one, array_two)); //logs false to the console, was expecting 'true'.
Ptrades
  • 57
  • 4

2 Answers2

3
  1. Using some method to check if any value from one array exists in another array:

const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.some(value => arr2.includes(value));
  }
};

console.log(checkArray(array_one, array_two)); //true
  1. Using filter method:

const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.filter(value => arr2.includes(value)).length > 0;
  }
};

console.log(checkArray(array_one, array_two)); //true
  1. Using the some method with the Set which is more performant since it has an average time complexity of O(n) for the has method, and does not require iterating through the entire array.

const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    const set = new Set(arr2);
    return arr1.some(value => set.has(value));
  }
};

console.log(checkArray(array_one, array_two)); //true
  1. Using the reduce method:

const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.reduce((accumulator, value) => accumulator || arr2.includes(value), false);
  }
};

console.log(checkArray(array_one, array_two)); //true

Note: methods 1. filter and 2. reduce are less performant , since they create new arrays, accumulate values, which can be memory-intensive and slow for larger arrays.

XMehdi01
  • 5,538
  • 2
  • 10
  • 34
0

Just flip your logic around.

  • Make false the default assumption
  • Create a Set for each input Array for O(1) lookup
  • Check if the current item in the set exists in the other and immediately return true

const
  array_one   = [ "Apparel" , "Footwear" ],
  array_two   = [ "Soap"    , "Footwear" ],
  array_three = [ "Apple"   , "Orange"   ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false; // lengths must match
  }
  const
    set1 = new Set(arr1),
    set2 = new Set(arr2);
  for (const [value] of set1.entries()) {
    if (set2.has(value)) {
      return true; // short-circuit with true
    }
  }
  return false; // default assumption
};

console.log(checkArray(array_one, array_two));   // true
console.log(checkArray(array_one, array_three)); // false

If you think Set is overkill, you can always use Array.prototype.includes:

const
  array_one   = [ "Apparel" , "Footwear" ],
  array_two   = [ "Soap"    , "Footwear" ],
  array_three = [ "Apple"   , "Orange"   ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false; // lengths must match
  }
  for (let i = 0; i < arr1.length; i++) {
    if (arr2.includes(arr1[i])) {
      return true; // short-circuit with true
    }
  };
  return false; // default assumption
};

console.log(checkArray(array_one, array_two));   // true
console.log(checkArray(array_one, array_three)); // false
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132