hii team,
please help me on below item:
we have 2 arrays with some values
array1 = {11,AB,**,67};
array2 = {44,67,11,**};
note : Arrays can contain any kind of data int,spl chars, strings
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

Amit MohantyRank #112 · 20K reputationPosted Oct 17, 2023, 6:48 AM EDT
var array1 = [11, 'AB', '**', 67]; var array2 = [44, 67, 11, '**']; // Convert the arrays to sets var set1 = new Set(array1); var set2 = new Set(array2); // Function to check if there are common values function haveCommonValues(setA, setB) { for (var item of setA) { if (setB.has(item)) { return true; } } return false; } var commonValuesExist = haveCommonValues(set1, set2); console.log(commonValuesExist);Naimish MakwanaRank #324 · 7.7K reputationPosted Oct 17, 2023, 7:04 AM EDT
Try below
// Define a function to check if two arrays have common elements function areArraysIntersecting(arr1, arr2) { const set1 = new Set(arr1); const set2 = new Set(arr2); return [...set1].some(item => set2.has(item)); } // Define the two arrays const array1 = [11, "AB", "**", 67]; const array2 = [44, 67, 11, "**"]; // Check if there are common elements const hasCommonElements = areArraysIntersecting(array1, array2); // Output the result console.log(hasCommonElements);Thanks