check if array elements are unique

I know that there are posts about this all over the place probably but the way my course was taught was quite unique. The professor has his own compiler that uses a simplified c++ language, so I may not even be able to be helped but I'll give it a shot.

Basically I have to ask for an input of 6 elements into an array, check if the elements are unique or not using nested WHILE loops (I specify while loops because we were never taught for or do loops, the for loop being the most common loop i saw when i looked around for help on this), and output "unique" or "not unique". I for the life of me just can't figure out how to do this, specifically what the controls for the loops would be or how to nest them.

Here is a screenshot of the code I have so far (since I can't copy and paste with his compiler): https://gyazo.com/04f5f5b6a2fa5e5fe5120484a7d5f346
All i have is a loop to input values into the array.

Any help is appreciated, thank you.
closed account (48T7M4Gy)
You should paste your code here. :)
kemort I did post it, the link is a screenshot. the compiler doesn't allow copying or pasting.

edit: heres the code.

#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
	     int myArray[6];
	     int counter=0;
	     while(counter<6){
		     cout<<"enter six numbers: ";
		     cin>>myArray[counter];
		     cout<<myArray[counter];
		     cout<<endl;
		     counter=(counter+1);
	     }
	     while(){
	     }
	     return 0;
}
Last edited on
closed account (48T7M4Gy)
Then type your code in here. It's only a few lines after all.
i edited it in to an above comment
closed account (48T7M4Gy)
Good. If you use code tags <> on the right, to enclose your code it ends up looking like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() 
{
   int myArray[6];
   int counter=0;
  while(counter<6){
  cout<<"enter six numbers: ";
  cin>>myArray[counter];
  cout<<myArray[counter];
  cout<<endl;
  counter=(counter+1);
}
return 0;
}


So, having said that do you have a plan of how to tackle the problem?
Last edited on
not exactly I legitimately don't know what do which is why im on a forum. I asked in class and i got that i can loop through one element at a time in the outside loop and then for each of those elements, i can check it against all of the other elements in the array. However my brain just can't wrap around how exactly to do this, and anywhere else I looked for some idea of what to do they all used for loops.
Last edited on
Okay, so you have a loop to enter the numbers. The next step is to see which ones are unique. By the way, do you have to wait until the user enters all the numbers, or can you check for uniqueness as they enter them?

To check whether myArray[counter] is unique, you need to compare it against the previously entered numbers. With a for loop that might be:
1
2
3
4
5
6
7
8
9
int idx;
for (idx = 0; idx < counter; ++idx) {
    if (myArray[idx] == myArray[counter]) break;
}
if (idx == counter) {
    cout << "unique\n";
} else {
    cout << "not unique\n";
}


Now just translate that for loop into a while loop and you're done.
i'm not sure. I'm assuming it would be after the user enters all the numbers. like i said the class was taught uniquely any type of online resource ive ever looked at for help just had things ive never seen before. I'm not even 100% sure of how to translate that for loop into a while loop. Also i was told there should be nested loops. So how you are showing me seems to be only a single loop which means thats not how i should be doing it.

edit: after looking at your solution and attempting to use it i don't even know how to implement that it actually doesn't make sense to me
Last edited on
closed account (48T7M4Gy)
You need to focus on what you are trying to achieve in general terms rather than what code you are going to write. That's why you're being pushed around by the wind going nowhere.

Ask yourself if you had 6 items sitting on a table, how would you test whether they are all unique. And then you need to decide what unique means in this.

Well, if it was me, I would
pick each one up in turn and compare it with all the others.
If that one is the same as any of the others then it isn't unique.
I'd continue through the collection of items until they've all been tested.


What you need to do now is refine that plan a bit if necessary so that you have a clear understanding of your solution prior to writing code for each of the plan steps.

BTW There are tutorials here with a section on control structures if, for, while etc with working examples. You need them! Your programming career will get a huge boost if you spend a couple of hours on that section. http://www.cplusplus.com/doc/tutorial/control/
so I've gotten somewhere kind of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main() {
	int myArray[6];
	int counter=0;
	int counter2=0;
	int counter3;
	while(counter<6){
		cout<<"enter six numbers: ";
		cin>>myArray[counter];
		cout<<myArray[counter];
		cout<<endl;
		counter=(counter+1);
	}
	while(counter2<counter){
		counter3=0;
		while(counter3<counter){
			counter3=(counter3+1);
		}
		counter2=(counter2+1);
	}
	return 0;
}


I need to utilize that nested while loop setup to check for duplicates but im still at a loss as to how i can.
closed account (48T7M4Gy)
You still don't get it. Have you read the tutorials? What is you plan? If you have one then post it. If you haven't then why are you wasting your time? This is not a homework site and your expectation that we will write your code is unfounded.

PS Your so-called professor isn't using any 'special' C++.
Topic archived. No new replies allowed.