hi team,
please help me on below item:
we have 2 arrays with some values
array1 = {11,34,54,67};
array2 = {44,67,11,456};
here need to compare the 2 arrays irrespective of their position... if any value are common then need to throw true/false
please help me

Vishal JoshiRank #377 · 6.7K reputationPosted Oct 17, 2023, 6:02 AM EDT
Hello Murali,
Please check out the below code to intersect the array and find common. if there are common values it will return true or false.
var array1 = [1, 2, 3, 4], array2 = [4, 5, 6]; $.arrayIntersect = function(a, b) { return $.grep(a, function(i) { return $.inArray(i, b) > -1; }); }; let existValues = $.arrayIntersect(array1, array2).length > 0; window.console && console.log(existValues);Thanks
Vishal Joshi
Amit MohantyRank #112 · 20K reputationPosted Oct 17, 2023, 5:33 AM EDT
$(document).ready(function () { var array1 = [11, 34, 54, 67]; var array2 = [44, 67, 11, 456]; function haveCommonValues(arr1, arr2) { return arr1.some(value => arr2.includes(value)); } var commonValuesExist = haveCommonValues(array1, array2); console.log(commonValuesExist); });Naimish MakwanaRank #324 · 7.7K reputationPosted Oct 17, 2023, 7:06 AM EDT
Try below
// Define the two arrays const array1 = [11, 34, 54, 67]; const array2 = [44, 67, 11, 456]; // Create a function to check for common elements function haveCommonElements(arr1, arr2) { const set1 = new Set(arr1); for (const item of arr2) { if (set1.has(item)) { return true; // If a common element is found, return true } } return false; // If no common elements are found, return false } // Check if there are common elements const hasCommonElements = haveCommonElements(array1, array2); // Output the result console.log(hasCommonElements);Thanks