Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar
Three smallest numbers
MédiaArrays10 min
Write a function threeSmallest(numbers) that returns the three smallest numbers of an array.
numbersis an array of integers. They can be positive, negative or0.- Return a new array with the three smallest numbers, from small to large.
- Equal numbers count separately:
[3, 3, 1]gives[1, 3, 3]. - If the array has fewer than 3 numbers, return all of them, from small to large. An empty array gives
[]. - Compare the numbers by value:
10is larger than9. - Do not change the input array.
If you sort, keep in mind that sort changes (mutates) the array it is called on and needs a compare function to sort numbers by value.
Exemplos
threeSmallest([5, 1, 4, 2, 3])Retorna [ 1, 2, 3 ]
threeSmallest([3, 3, 1])Retorna [ 1, 3, 3 ]
threeSmallest([8, 2])Retorna [ 2, 8 ]
Carregando o editor...
- Entrada
threeSmallest([ 5, 1, 4, 2, 3 ])Esperado[ 1, 2, 3 ] - Entrada
threeSmallest([ 3, 3, 1 ])Esperado[ 1, 3, 3 ] - Entrada
threeSmallest([ 8, 2 ])Esperado[ 2, 8 ]
Os testes ocultos rodam no envio