Python Nozioni di baseFacile

Trovare la frequenza degli elementi in un array

Guida dettagliata e implementazione Python per il problema 'Trovare la frequenza degli elementi in un array'.

Dichiarazione del problema

Facile

Scrivi una funzione frequency(arr) che accetta un elenco di numeri interi arr e restituisce un dizionario in cui ogni chiave è un elemento dell'array e ogni valore è il numero di volte in cui quell'elemento appare. Restituisce il dizionario ordinato per chiavi in ​​ordine crescente.

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

Esempi

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

1 appears once, 2 appears twice, 3 appears three times.

Example 2
Input
arr = [5, 5, 5, 5]
Output
{5: 4}
Explanation

5 appears four times.

Example 3
Input
arr = [4, 1, 2, 1, 4]
Output
{1: 2, 2: 1, 4: 2}
Explanation

1 appears 2 times, 2 appears 1 time, 4 appears 2 times.

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.