/*
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.
*/
#include <iostream>
using namespace std;
void sort( int * arr, int n) {
int min,pos, aux;
for ( int i = 0 ; i < n - 1 ; i++ ) {
min = arr[ i] ;
pos = i;
for ( int j = i + 1 ; j < n; ++ j) {
if ( arr[ j] < min) {
min = arr[ j] ;
pos = j;
}
}
aux = arr[ pos] ;
arr[ pos] = arr[ i] ;
arr[ i] = aux;
}
}
int main( int argc, char const * argv[ ] ) {
int arr[ ] = { 1 ,2 ,3 ,2 ,5 ,- 41 ,7 ,8 ,- 41 ,15 ,90 ,- 41 ,2 ,3 ,5 ,8 } ,
n = sizeof ( arr) / sizeof ( arr[ 0 ] ) ;
int freq[ 100 ] , vec[ 100 ] , k = 0 ;
for ( int i = 0 ; i < n - 1 ; ++ i) cout << arr[ i] << " " ;
sort( arr, n) ;
vec[ k] = arr[ k] ;
freq[ k] = 1 ;
//display sorted
//for(int i = 0; i < n - 1; ++i) cout<<arr[i]<<" ";
for ( int i = 0 ; i < n - 1 ; ++ i) {
if ( arr[ i] == arr[ i+ 1 ] ) {
freq[ k] ++ ;
} else {
k++ ;
vec[ k] = arr[ i+ 1 ] ;
freq[ k] = 1 ;
}
}
cout << "\n " ;
for ( int i = 0 ; i < k; i++ ) {
if ( freq[ i] == 1 ) {
cout << "Elementul " << vec[ i] << " apare o singura data" << "\n " ;
} else
cout << "Elementul " << vec[ i] << " apare de " << freq[ i] << " ori!" << "\n " ;
}
return 0 ;
}
LyoKRmlpbmQgZGF0IHZlY3RvcnVsIFggY3UgbiBlbGVtZW50ZSBpbnRyZWdpLCBzYSBzZSBhZmlzZXplCmluIG9yZGluZWEgY3Jlc2NhdG9hcmUgYSBlbGVtZW50ZWxvciBjb21wb25lbnRlb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