Le migliori 150 intervisteFacile

Trova il numero duplicato

Guida dettagliata e implementazione Python per il problema "Trova il numero duplicato".

Dichiarazione del problema

Facile

Dato un array di numeri interi contenente n + 1 numeri interi dove ciascun numero intero è compreso nell'intervallo [1, n] compreso.

C'è solo un numero ripetuto in num, restituisci questo numero ripetuto.

È necessario risolvere il problema senza modificare i numeri dell'array e utilizzando solo spazio aggiuntivo costante.

Implementa una funzione findDuplicate(nums: list) -> int che restituisce il numero duplicato.

Vincoli
  • 1 <= n <= 100000
  • nums.length == n + 1
  • 1 <= nums[i] <= n
  • There is only one repeated number in nums, but it could be repeated more than once

Esempi

Example 1
Input
[1,3,4,2,2]
Output
2
Explanation

The duplicate number is 2. It appears twice in the array.

Example 2
Input
[3,1,3,4,2]
Output
3
Explanation

The duplicate number is 3. It appears twice in the array.

Example 3
Input
[3,3,3,3,3]
Output
3
Explanation

The duplicate number is 3. It appears five times.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche dell'elenco collegato come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.