Python Nozioni di baseFacile

Conteggio di elementi distinti in una matrice

Guida dettagliata e implementazione Python per il problema "Conteggio di elementi distinti in un array".

Dichiarazione del problema

Facile

Scrivi una funzione count_distinct(arr) che accetta un elenco di numeri interi arr e restituisce il conteggio di elementi distinti (unici) nell'array.

Vincoli
  • 0 <= len(arr) <= 10^5
  • -10^6 <= arr[i] <= 10^6

Esempi

Example 1
Input
arr = [1, 2, 2, 3, 3, 3]
Output
3
Explanation

The distinct elements are 1, 2, and 3. Count = 3.

Example 2
Input
arr = [5, 5, 5, 5]
Output
1
Explanation

Only one distinct element: 5.

Example 3
Input
arr = [1, 2, 3, 4, 5]
Output
5
Explanation

All elements are distinct.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche degli array 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.