Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar
Common elements of two lists
MédiaMap and Set10 min
Write a function commonElements(listA, listB) that returns the elements that are in both lists: their intersection.
listAandlistBare arrays of strings. They can be empty and can have duplicates.- Return a new array with every common element once, even if it occurs several times in one or both lists.
- Order: the order of the first occurrence in
listA. - Case matters:
'ann'and'Ann'are different elements. - If there are no common elements, return an empty array.
A Set helps to check fast whether a value is in a list and to remember what is already in the result.
Exemplos
commonElements(['ann', 'bob', 'carl', 'dan'], ['dan', 'eve', 'bob'])Retorna [ 'bob', 'dan' ]
commonElements(['ann', 'ann', 'bob'], ['ann'])Retorna [ 'ann' ]
commonElements(['ann'], ['Ann'])Retorna []
Cobrada em Ness Digital · Fonte
Carregando o editor...
- Entrada
commonElements([ 'ann', 'bob', 'carl', 'dan' ], [ 'dan', 'eve', 'bob' ])Esperado[ 'bob', 'dan' ] - Entrada
commonElements([ 'red', 'green' ], [ 'blue', 'green' ])Esperado[ 'green' ] - Entrada
commonElements([ 'a', 'b' ], [ 'c', 'd' ])Esperado[]
Os testes ocultos rodam no envio