Find unequal elements in array
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Find unequal elements in array

1. Junior Member
Join Date
Jan 2014
Posts
3

## Find unequal elements in array

Hi,
I would like to know the algorithm or flow for finding unequal elements in an array entered by user. I tried using for loop but didn't get the expected output
Code:
```#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[10],b[10]; //array of integer elements
int i,j,k ; //traversing operators
int r =0;
cout<<"Enter Array Elements = ";
for(i=0;i<=9;i++)
cin>>a[i];

cout<<"The common elements are ";
//For to loop to compare all elements and find unique elements
for(j=0;j<=9;j++)
{

for(k=j;k<9;k++)
{if (a[k] != a[j] && k!=j)
r++;

break;
}

}
cout<<"The number of unique elements in the array="<<r;

return 0;
}```
Thanks for time and consideration awaiting response.

2. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Find unequal elements in array

Why do you have a break statement in your inner loop?

3. Junior Member
Join Date
Jan 2014
Posts
3

## Re: Find unequal elements in array

Originally Posted by GCDEF
Why do you have a break statement in your inner loop?
To increment the counter in case of unequal elements and come out of the inner loop, plz correct me if I am wrong
Thanks.

4. ## Re: Find unequal elements in array

If you want to find the unique elements, it would be easier if you used a map if you want to know how many if each were entered or a set if you just want to know the unique numbers and how many unique there are.

5. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Find unequal elements in array

Originally Posted by fighter29
To increment the counter in case of unequal elements and come out of the inner loop, plz correct me if I am wrong
Thanks.
Your inner loop never runs more than once because of the break statement.

6. Member
Join Date
Aug 1999
Location
Darmstadt, FRG
Posts
87

## Re: Find unequal elements in array

The problem you are trying to solve is quite universal. You might find a solution in almost every textbook on c++, e.g.: TC++PL, 4th edition Section 32.5.2., p.937

7. Junior Member
Join Date
Jan 2014
Posts
3

## Re: Find unequal elements in array

Originally Posted by greve
The problem you are trying to solve is quite universal. You might find a solution in almost every textbook on c++, e.g.: TC++PL, 4th edition Section 32.5.2., p.937
thanks a lot

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)