0772 - Max Ap

From Bitnami MediaWiki
Revision as of 18:36, 3 April 2023 by MiclausIoana (talk | contribs) (Pagină nouă: == Rezolvare == <syntaxhighlight lang="python" line="1"> n, m = map(int, input().split()) # citim n si m de la tastatura matrix = [] # initializam matricea for i in range(n): row = list(map(int, input().split())) # citim o linie a matricei matrix.append(row) # adaugam linia la matrice counts = {} # initializam un dictionar pentru a numara aparitiile elementelor max_count = 0 # initializam numarul maxim de aparitii la 0 for row in matrix: for elem in row:...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Rezolvare

<syntaxhighlight lang="python" line="1"> n, m = map(int, input().split()) # citim n si m de la tastatura matrix = [] # initializam matricea for i in range(n):

   row = list(map(int, input().split()))  # citim o linie a matricei
   matrix.append(row)  # adaugam linia la matrice

counts = {} # initializam un dictionar pentru a numara aparitiile elementelor max_count = 0 # initializam numarul maxim de aparitii la 0

for row in matrix:

   for elem in row:
       if elem in counts:
           counts[elem] += 1  # incrementam numarul de aparitii pentru un element existent
       else:
           counts[elem] = 1  # adaugam un element nou in dictionar cu numarul de aparitii initializat la 1
       if counts[elem] > max_count:
           max_count = counts[elem]  # actualizam numarul maxim de aparitii

max_elems = sorted([elem for elem, count in counts.items() if count == max_count]) # determinam toate elementele cu numarul maxim de aparitii si le sortam in ordine crescatoare

print(*max_elems) # afisam elementele separate prin spatii </syntaxhighlight>