Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado
Check two arrays for a common element
MediaMap and Set10 min
Write a function hasCommonElement(a, b) that checks if two arrays have at least one equal element. In other words, it checks that their intersection is not empty.
- Every element of
aandbis a number or a string. The arrays can be empty and can have repeated elements. - Return a boolean:
trueif some element ofais also inb, otherwisefalse. - Compare with strict equality
===: the number1and the string'1'are different, and'a'and'A'are different. - The equal elements can be at any positions in the two arrays.
- If one of the arrays is empty, return
false.
A set of the elements of b makes the check fast for long arrays.
Ejemplos
hasCommonElement([1, 2, 3], [3, 4])Devuelve true
hasCommonElement([1, 2], [3, 4])Devuelve false
hasCommonElement([1, 2], ['1'])Devuelve false
Cargando el editor…
- Entrada
hasCommonElement([ 1, 2, 3 ], [ 3, 4 ])Esperadotrue - Entrada
hasCommonElement([ 1, 2 ], [ 3, 4 ])Esperadofalse - Entrada
hasCommonElement([], [ 1, 2 ])Esperadofalse
Las pruebas ocultas se ejecutan al enviar