Nexo is optimizing the duplicate identifier detector at Código con Criterio and wants to contrast a direct nested-loop approach against a Set-based approach.
Implement the required functions buscarDuplicadosDirecto(numeros), buscarDuplicadosSet(numeros), and solve(input), which receives:
numeros: array of 0 to 500 integers between -10000 and 10000 (not necessarily sorted, duplicates allowed).
buscarDuplicadosDirecto(numeros) Requirements
Create an array duplicados = [] and a counter comparaciones = 0.
Iterate with an outer for loop i from 0 to n - 1 and an inner for loop j from i + 1 to n - 1.
On each inner iteration, increment comparaciones by 1 (includes() calls are not added to comparaciones).
If numeros[i] === numeros[j]:
Push numeros[i] to duplicadosonly if !duplicados.includes(numeros[i]).
Execute break to exit the inner loop and continue with the next i.
Finally, sort duplicados in ascending numeric order via sort((a, b) => a - b) and return { duplicados, comparaciones }.
buscarDuplicadosSet(numeros) Requirements
Create two sets: vistos = new Set() and duplicados = new Set(), plus operaciones = 0.
Iterate through numeros once using for...of, incrementing operaciones by 1 for each visited number.
If vistos.has(numero) is true, add it to duplicados with add(numero); otherwise, add it to vistos with add(numero).
Convert duplicados to an array with Array.from(duplicados), sort it in ascending numeric order via sort((a, b) => a - b), and return { duplicados, operaciones }.
Coordination in solve(input)
Run both functions independently on input.numeros (without mutating the input) and return an object with exactly seven properties:
duplicados: ascending sorted array of duplicate values (each duplicate value listed once).
cantidadDuplicados: length of duplicados.
comparacionesDirecto: counter from buscarDuplicadosDirecto.
operacionesSet: counter from buscarDuplicadosSet (always equal to numeros.length).
estrategiaMenorTrabajo: "DIRECTA" if comparacionesDirecto < operacionesSet, "SET" if operacionesSet < comparacionesDirecto, or "EMPATE" if equal.