Find Common Elements in JavaScript Arrays in JavaScript | Learn JavaScript & Algorithms

preview_player
Показать описание
Find common elements in javascript arrays. Write a function in javascript to find the common elements in two arrays in JavaScript. It is a common javascript interview question. There are a few ways to do it using javascript. You can use a for loop to compare each element of the first array with the second array, if the element is found in the second array, then add that element to a new array. Lets find out how to find common elements in two arrays in javascript.

Let's define a function called "commonElem" that takes two parameters "arr1" and "arr2", which are arrays. Inside function, let's declare an empty array called "common", which will be used to store the common elements found in both arrays. At the end it will return the common elements. Use a for loop to iterate through each element of the first array, using the variable "i" to represent the current index. For each iteration of the loop, use the indexOf method on the second array to check if the current element of the first array exists in the second array. The indexOf method returns the first index at which a given element can be found in an array, or -1 if it is not present. If the current element of the first array is present in the second array, use the push method to add that element to the "common" array. Once the loop has completed, return the "common" array, which contains all the elements that were found in both the first and second arrays. Call the function with two arrays as the parameters, and function will return an array that contains the common elements between the given arrays.

Alternatively, use the set data structure, 'has method' and filter method to check if the current element of the first array is present in the second array. To do that let's define a function called "commonElems" that takes two parameters, which are arrays. let's create a new Set object called "set2" using the new Set constructor, which converts the elements of the second array into a set.

A Set is a collection of unique values.

Then, use the filter method on the first array, which creates a new array with all elements that pass the test implemented by the provided function. In this case, the function passed to filter checks if the current value 'val' is present in the set2 using the has method. The has method returns true if the element is present in the set, otherwise it returns false. The result of the filter method will be an array containing only the elements that are found in both arrays. Call the function with the arrays as the parameters, and the function will return an array that contains the common elements between both arrays.

This approach uses the set data structure for the second array and provides an O(1) lookup time
for checking if an element is present in the second array which makes it more efficient than using a for loop and the indexOf method as it eliminates the need for looping through the array and searching for the elements. O(1) lookup time refers to an algorithm that takes a constant amount of time to complete regardless of the size of the input.

There is another way to do it. Define a function that takes two arrays as arguments. Call the function and provide it arrays. Use the filter method on the first array. It will create a new array with all elements that pass the test implemented by the provided function. In this case, the function passed to filter checks if the current value 'val' is included in the second array using the 'includes' method. The result of the filter method will be an array containing only the elements that are found in both arrays.

So this is how we can find common elements in arrays using javascript.

* Full Playlist (Coding Challenge, Interview Questions & Leetcode) *

It can be a good javascript interview question or frontend interview question.

Our tutorials help you to improve your career growth, perform better in your job and make money online as a freelancer. Learn the skills to build and design professional websites, and create dynamic and interactive web applications using JavaScript, or WordPress. Our tutorials are tailored to help beginners and professionals alike. Whether you're just starting in the field or you're looking to expand your knowledge, we've got something for you. Join us on this journey to becoming a skilled web developer. Subscribe to our channel and let's get started!

Thank You!
👍 LIKE VIDEO
👊 SUBSCRIBE
🔔 PRESS BELL ICON
✍️ COMMENT

#js #javascript #challenge #WebStylePress #WebDevelopment #javascriptinterviewquestions #javascripttutorial #leetcode #coding #programming #computerscience #array
Рекомендации по теме