'''
Fiind dat vectorul X cu n elemente intregi, sa se afiseze
in ordinea crescatoare a elementelor componentelor distincte si a
frecventelor lor de aparitie.
Data de intrare: n = 6; X = [45, -90, 45, 2, 25, 2]
Date de iesire: Elementul -90 apare o data.
Elementul 2 apare de 2 ori.
Elementul 45 apare de 3 ori.
'''
def sort( arr, n) :
for i in range ( 0 , n - 1 ) :
min = arr[ i]
pos = i
for j in range ( i + 1 , n) :
if arr[ j] < min :
min = arr[ j]
pos = j
aux = arr[ pos]
arr[ pos] = arr[ i]
arr[ i] = aux
def main( ) :
arr = [ 1 , 2 , 3 , 2 , 5 , -41 , 7 , 8 , -41 , 15 , 90 , -41 , 2 , 3 , 5 , 8 ]
print ( arr)
vec = [ 0 ] * 1000
k = 0
freq = [ 0 ] * 100
n = len ( arr)
sort( arr, n)
k = 0
vec[ k] = arr[ 0 ]
freq[ k] = 1
for i in range ( 0 , n - 1 ) :
if arr[ i] == arr[ i+1 ] :
freq[ k] += 1
else :
k += 1
vec[ k] = arr[ i+1 ]
freq[ k] = 1
for i in range ( 0 , k) :
if freq[ i] == 1 :
print ( "Elementul " , vec[ i] , " apare o singura data!" )
else :
print ( "Elementul " , vec[ i] , " apare de " , freq[ i] , " ori!" )
main( )
JycnCkZpaW5kIGRhdCB2ZWN0b3J1bCBYIGN1IG4gZWxlbWVudGUgaW50cmVnaSwgc2Egc2UgYWZpc2V6ZQppbiBvcmRpbmVhIGNyZXNjYXRvYXJlIGEgZWxlbWVudGVsb3IgY29tcG9uZW50ZWxvciBkaXN0aW5jdGUgc2kgYQpmcmVjdmVudGVsb3IgbG9yIGRlIGFwYXJpdGllLgoKRGF0YSBkZSBpbnRyYXJlOiBuID0gNjsgWCA9IFs0NSwgLTkwLCA0NSwgMiwgMjUsIDJdCkRhdGUgZGUgaWVzaXJlOiBFbGVtZW50dWwgLTkwIGFwYXJlIG8gZGF0YS4KICAgICAgICAgICAgICAgIEVsZW1lbnR1bCAyIGFwYXJlIGRlIDIgb3JpLgogICAgICAgICAgICAgICAgRWxlbWVudHVsIDQ1IGFwYXJlIGRlIDMgb3JpLgonJycKZGVmIHNvcnQoYXJyLCBuKToKICAgIGZvciBpIGluIHJhbmdlKDAsIG4gLSAxKToKICAgICAgICBtaW4gPSBhcnJbaV0KICAgICAgICBwb3MgPSBpCiAgICAgICAgZm9yIGogaW4gcmFuZ2UoaSArIDEsIG4pOgogICAgICAgICAgICBpZiBhcnJbal0gPCBtaW46CiAgICAgICAgICAgICAgICBtaW4gPSBhcnJbal0KICAgICAgICAgICAgICAgIHBvcyA9IGoKICAgICAgICBhdXggPSBhcnJbcG9zXQogICAgICAgIGFycltwb3NdID0gYXJyW2ldCiAgICAgICAgYXJyW2ldID0gYXV4CgpkZWYgbWFpbigpOgogICAgYXJyID0gWzEsMiwzLDIsNSwtNDEsNyw4LC00MSwxNSw5MCwtNDEsMiwzLDUsOF0KICAgIHByaW50KGFycikKICAgIHZlYyA9IFsgMCBdICogMTAwMAogICAgayA9IDAKICAgIGZyZXEgPSBbIDAgXSAqIDEwMAogICAgbiA9IGxlbihhcnIpCiAgICBzb3J0KGFyciwgbikKICAgIGsgPSAwCiAgICB2ZWNba10gPSBhcnJbMF0KICAgIGZyZXFba10gPSAxCiAgICBmb3IgaSBpbiByYW5nZSgwLCBuIC0gMSk6CiAgICAgICAgaWYgYXJyW2ldID09IGFycltpKzFdOgogICAgICAgICAgICBmcmVxW2tdICs9IDEKICAgICAgICBlbHNlOgogICAgICAgICAgICBrICs9IDEKICAgICAgICAgICAgdmVjW2tdID0gYXJyW2krMV0KICAgICAgICAgICAgZnJlcVtrXSA9IDEKICAgIGZvciBpIGluIHJhbmdlKDAsIGspOgogICAgICAgIGlmIGZyZXFbaV0gPT0gMToKICAgICAgICAgICAgcHJpbnQoIkVsZW1lbnR1bCAiLCB2ZWNbaV0sICIgYXBhcmUgbyBzaW5ndXJhIGRhdGEhIikKICAgICAgICBlbHNlOgogICAgICAgICAgICBwcmludCgiRWxlbWVudHVsICIsIHZlY1tpXSwgIiBhcGFyZSBkZSAiLCBmcmVxW2ldLCAiIG9yaSEiKQoKbWFpbigpCg==
stdout
[1, 2, 3, 2, 5, -41, 7, 8, -41, 15, 90, -41, 2, 3, 5, 8]
Elementul -41 apare de 3 ori!
Elementul 1 apare o singura data!
Elementul 2 apare de 3 ori!
Elementul 3 apare de 2 ori!
Elementul 5 apare de 2 ori!
Elementul 7 apare o singura data!
Elementul 8 apare de 2 ori!
Elementul 15 apare o singura data!