Common elements
JavaScript
Easy
Objectives
Write a function that returns the common elements between two arrays
Requirements
Create a function called
commonElements
It should take two arrays as parameters
Do not use built in methods like
filter
orincludes
The function should return an array containing the elements that appear in both input arrays
If there are duplicate common elements, they should appear as many times as they appear in both arrays
If there are no common elements, the function should return an empty array
Check your solution
Tests
Function name is commonElements | |||
commonElements([1,2,3],[3,4,5]) | Expected: | [3] | |
commonElements([1,2,3],[4,5,6]) | Expected: | [] | |
commonElements([1,1,2,2],[1,2,2,3]) | Expected: | [1,2,2] |