Common elements

JavaScript
Easy

Objectives

Write a function that returns the common elements between two arrays

Requirements

  1. Create a function called commonElements

  2. It should take two arrays as parameters

  3. Do not use built in methods like filter or includes

  4. The function should return an array containing the elements that appear in both input arrays

  5. If there are duplicate common elements, they should appear as many times as they appear in both arrays

  6. 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]