# Thread: number of non same elements

1. ## number of non same elements

hi
this program is for counting number of nons ame elements in an array of size 5
i think this program is specific for array of size 5 this is not general
i want to make a general program, means by changing just 1 or 2 values in the code i can use it for higher size arrays here is my code
Code:
```# include <iostream>
# include <conio.h>
# include <windows.h>
using namespace std;
int main ()
{
int a[10];

char push_key='#';
while (push_key!='@')
{
double n=0;
for (int i=0;i<5;i++)
{
cout<<"enter number";
cin>>a[i];

}//end for
for (int i=0;i<5;i++)
{
for (int j=i+1;j<5;j++)
{
if (a[i]==a[j])
{

n++;

}//end if

}//end for
if (n==4)
{
break;

}//end if

}//end for
if (n==4){
cout<<"total non same elements are:"<<4-n<<endl;
cout<<endl<<endl;
}//end if
if (n<4)
{
cout<<"total non same elements are:"<<5-n<<endl;
cout<<endl<<endl;

}//end if
if (n>4)
{```
any help will b appreciated thanks

2. Originally Posted by rafay_07
this program is for counting number of nons ame elements in an array of size 5
What exactly do you mean by "number of non-same elements"? For example, suppose we have an array consisting of two 1s and three 2s. How many elements are "non-same"?

3. let me explain
suppose i have an array of size 5 and i enter these number
1 2 2 4 5
here 1, 4,5 are not same so i say number of non same elements is 3 while number of same elements is 2
so i want to display number of non same elements in the array

4. hmm... well, if I understand this right, I would suggest that you make use of a std::map<int, size_t>. You just insert into the map and increment the value (i.e., map the elements to their counts). When you are done, you iterate over the map. All keys for which the values are 1 are "non-same".

5. Let an integer 'x' equal some number of interest, and let integer 'k' equal zero. Then in a loop from j = 0 to array.size()-1 perform a check against each (array[j] == x); If true do nothing, if not true increment k by one. When the loop completes 'k' will represent the number of elements which are not the same as some given value 'x'.

I figure that's not a complete solution for what you're looking to do, but it is a start.

In a problem like this, especially if you're new to algorithms/math/programming, what I recommend you do is you write out the steps that are necessary to complete a task, make it work on paper. Then use what you know of some programming language to implement those steps for a program to execute. Sometimes it helps to think 'out loud' on paper before writing code, that's usually a good way of figuring these things out on your own.