Code Lab

Modo entrevista. Temporizador de 15 min, sin pistas y sin autocompletado

Indexes of duplicate numbers

DifícilMap and Set15 min

Write a function duplicateIndices(numbers) that finds where every duplicate number stands in an array.

  • numbers is an array of integers. They can be positive, negative or 0.
  • 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.

Ejemplos

duplicateIndices([3, 1, 3, 2, 1, 3])

Devuelve Map(2) { 3 => [ 0, 2, 5 ], 1 => [ 1, 4 ] }

duplicateIndices([7, 8, 7])

Devuelve Map(1) { 7 => [ 0, 2 ] }

duplicateIndices([1, 2, 3])

Devuelve Map(0) {}

Preguntada en ServiceNow · Fuente

Cargando el editor…
  1. EntradaduplicateIndices([ 3, 1, 3, 2, 1, 3 ])
    EsperadoMap(2) { 3 => [ 0, 2, 5 ], 1 => [ 1, 4 ] }
  2. EntradaduplicateIndices([ 7, 8, 7 ])
    EsperadoMap(1) { 7 => [ 0, 2 ] }
  3. EntradaduplicateIndices([ 1, 2, 3 ])
    EsperadoMap(0) {}

Las pruebas ocultas se ejecutan al enviar