Write a function commonElements(listA, listB) that returns the elements that are in both lists: their intersection.
listA and listB are arrays of strings. They can be empty and can have duplicates.listA.'ann' and 'Ann' are different elements.A Set helps to check fast whether a value is in a list and to remember what is already in the result.
commonElements(['ann', 'bob', 'carl', 'dan'], ['dan', 'eve', 'bob'])Returns [ 'bob', 'dan' ]
commonElements(['ann', 'ann', 'bob'], ['ann'])Returns [ 'ann' ]
commonElements(['ann'], ['Ann'])Returns []
Asked at Ness Digital · Source
commonElements([ 'ann', 'bob', 'carl', 'dan' ], [ 'dan', 'eve', 'bob' ])[ 'bob', 'dan' ]commonElements([ 'red', 'green' ], [ 'blue', 'green' ])[ 'green' ]commonElements([ 'a', 'b' ], [ 'c', 'd' ])[]Hidden tests run on Submit.