Reputation: 1
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL,"Turkish");
int dizi[10]={9,30,3,6,9,20,3,6,10,9};
int counter, j;
for(int i=0; i<10; i++){
counter=1;
for(int j=i+1; j<10; j++){
if(dizi[i]==dizi[j+1]){
counter++;
}
}
cout<<dizi[i]<<"\t:"<<counter<<endl;
}
}
/*
//THE RESULT
9 :3
30 :1
3 :2
6 :2
9 :2 //WRONG I don't want this line to appear
20 :1
3 :1 //WRONG I don't want this line to appear
6 :1 //WRONG I don't want this line to appear
10 :1
9 :1 //WRONG I don't want this line to appear
*/
Upvotes: 0
Views: 93
Reputation: 16449
I prefer to use the STL for such problems. Iterate over your array, count the occurence in counts
and store the order in order
.
#include <iostream>
#include <unordered_map>
#include <vector>
int main()
{
int dizi[] = { 9, 30, 3, 6, 9, 20, 3, 6, 10, 9 };
std::unordered_map<int, std::size_t> counts;
std::vector<int> order;
for (const auto &el : dizi) {
if (counts.find(el) == counts.end()) order.push_back(el);
++counts[el];
}
for (const auto &el : order) {
std::cout << el << "\t:" << counts[el] << '\n';
}
return 0;
}
Output:
9 :3
30 :1
3 :2
6 :2
20 :1
10 :1
Upvotes: 2
Reputation: 310990
It seems you mean the following
#include <iostream>
int main()
{
int dizi[] = { 9, 30, 3, 6, 9, 20, 3, 6, 10, 9 };
const size_t N = sizeof( dizi ) / sizeof( *dizi );
for ( size_t i = 0; i < N; i++ )
{
size_t j = 0;
while ( j != i && dizi[j] != dizi[i] ) ++j;
if ( j == i )
{
size_t counter = 1;
while ( ++j != N )
{
if ( dizi[j] == dizi[i] ) ++counter;
}
std::cout << dizi[i] << ":\t" << counter << '\n';
}
}
return 0;
}
The program output is
9: 3
30: 1
3: 2
6: 2
20: 1
10: 1
That is you need to check whether a current value was already countered.
Upvotes: 0