Modo entrevista. Cronômetro de 15 min, sem dicas e sem autocompletar
Indexes of duplicate numbers
DifícilMap and Set15 min
Write a function duplicateIndices(numbers) that finds where every duplicate number stands in an array.
numbersis an array of integers. They can be positive, negative or0.- Return a Map. Each key is a number that occurs more than once. Its value is an array of all its indexes in
numbers, from small to large. - Indexes start at 0, as usual in JavaScript.
- A number that occurs only once is not in the Map.
- If there are no duplicates (also for an empty array), return an empty
Map. - Key order is not checked.
Exemplos
duplicateIndices([3, 1, 3, 2, 1, 3])Retorna Map(2) { 3 => [ 0, 2, 5 ], 1 => [ 1, 4 ] }
duplicateIndices([7, 8, 7])Retorna Map(1) { 7 => [ 0, 2 ] }
duplicateIndices([1, 2, 3])Retorna Map(0) {}
Cobrada em ServiceNow · Fonte
Carregando o editor...
- Entrada
duplicateIndices([ 3, 1, 3, 2, 1, 3 ])EsperadoMap(2) { 3 => [ 0, 2, 5 ], 1 => [ 1, 4 ] } - Entrada
duplicateIndices([ 7, 8, 7 ])EsperadoMap(1) { 7 => [ 0, 2 ] } - Entrada
duplicateIndices([ 1, 2, 3 ])EsperadoMap(0) {}
Os testes ocultos rodam no envio