Technicalarticles
Input: array1 = ['a', 'b', 'c', 'd', 'e'] array2 = ['f', 'g', 'c']Output: trueInput: array1 = ['x', 'y', 'w', 'z'] array2 = ['m', 'n', 'k']Output: false
<script> // Declare two array const array1 = ['a', 'b', 'c', 'd']; const array2 = ['k', 'x', 'z']; // Function definiton with passing two arrays function findCommonElement(array1, array2) { // Loop for array1 for(let i = 0; i < array1.length; i++) { // Loop for array2 for(let j = 0; j < array2.length; j++) { // Compare the element of each and // every element from both of the // arrays if(array1[i] === array2[j]) { // Return if common element found return true; } } } // Return if no common element exist return false; } document.write(findCommonElement(array1, array2)) </script>false
<script> // Declare Two array const array1 = ['a', 'd', 'm', 'x']; const array2 = ['p', 'y', 'k']; // Function call function findCommonElements2(arr1, arr2) { // Create an empty object let obj = {}; // Loop through the first array for (let i = 0; i < arr1.length; i++) { // Check if element from first array // already exist in object or not if(!obj[arr1[i]]) { // If it doesn't exist assign the // properties equals to the // elements in the array const element = arr1[i]; obj[element] = true; } } // Loop through the second array for (let j = 0; j < arr2.length ; j++) { // Check elements from second array exist // in the created object or not if(obj[arr2[j]]) { return true; } } return false; } document.write(findCommonElements2(array1, array2)) </script>false
<script> // Declare two array const array1= ['a', 'b', 'x', 'z']; const array2= ['k', 'x', 'c'] // Iterate through each element in the // first array and if some of them // include the elements in the second // array then return true. function findCommonElements3(arr1, arr2) { return arr1.some(item => arr2.includes(item)) } document.write(findCommonElements3(array1, array2)) </script> true
DO U LIKE?