program to check for duplicates in a string

Hello again,
posting once again for help...Here is my code. It is supposto recieve 10 numbers and outpu the duplicates.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//inputs, searches for duplicates
#include <iostream>
using namespace std;

int main()
{
	double input[10], duplicates[10];
	int l=0, o=0, y=0, n=0;

	cout << "Please enter 10 numbers, I will find and display the duplicates\n";
	for(int i=0; i<10; i++) cin >> input[i];

	cout << "Your numbers were:\n";
	for(int i=0; i<10; i++) cout << input[i] << ' ';

	
	for(;;){

		if(input[y] == input[o]){
			duplicates[n]=input[y];
			n++;
		}
		y++; //y is incremented in each pass
		if(y == 10){
			y=0;
			o++;
		}
		if(o == 10) break;
	}
			

	cout << "\nAnd the duplicates in this sequence are:\n";
	for(int i=0; duplicates[i]; i++) cout << duplicates[i] << '\n';

	return 0;
}


Thanks for taking a look!
enduser000
Last edited on
"...Make your subject line reflect your question well enough that the next guy
searching the archive with a question similar to yours will be able to follow the
thread to an answer rather than posting the question again..."

http://cplusplus.com/forum/articles/1295/
Last edited on
Here is the algorithm.

Have input array of size 10, have dupe array of size 5 (because you can't have more than 5 dupes)

int DupeAmt = 0; //records how many dupes are in the dupe array

Request for 10 numbers.

Open input loop, from 0 to 10. Call this InputCnt.
Each number is added to the input array.

Within the input loop, write a second loop that iterates through the existing inputs whose initial index is 0 and max is InputCnt (because we can only iterate through existing inputs) - call this DupeCnt

Check to see if the number you are currently adding ( input[InputCnt] ) is already on the input array.
Catch any dupe numbers. Do not place into Dupe array yet.

Init DupeLoop to 0 (outside of your loop)
Write a third loop to iterate through your dupe array to make sure that dupe doesn't already exist on there. It will start at 0 and end DupeCnt.

Check to see if the dupe number already exists on the dupe array, if it is, break.

End dupe check loop.

If the loop breaks out earlier than its end param of DupeCnt that means there must exist a dupe that already is on the list (because we use the 'break' keyword to break out of the loop if the dupe is found). If DupeLoop equals DupeCnt that means our loop finished without finding this dupe within our list.
if( DupeLoop == DupeCnt )
{
add this dupe to the dupe array
++DupeAmt;
}

Perform outputs of dupes iterating only to DupeAmt.

//---
Used only information that you have provided me - a faster implementation (but slightly more complex would be to use lists and insert sorting O(n))


Last edited on
Hey,
Thanks again all. I got the middle post, I'll be more thoughtful and put a meingful name on it next time. Thanks for continuing me along my start to programming. I'll post again.
enduser000
how do I check to see if the number has already been entered? I only know how to check adjacent numbers (or any number away, but not all)

that's one of the things I was trying to do in this one

thanks light,
enduser000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const int InArSize = 10;
const int DupeArSize = 5;

int InputArray[InArSize];
int DupeArray[DupeArSize];

for( int InputCnt = 0; InputCnt < InArSize; ++InputCnt)
{
    //ask for user input here

    int DupeCnt;

    //we're iterating through the beginning of our InputArray up to the point of
   //input count - this is how we check through the length of the array.
    for( DupeCnt = 0; DupeCnt <  InputCnt; ++DupeCnt )
    {
        if( InputArray[InputCnt] == InputArray[DupeCnt] )
          break;
    }

    //this will be true if this is a dupe
    if(  DupeCnt != InputCnt  )
   {
      //insert 3rd array here to check for dupes on the dupe array
   }
}

Last edited on
Topic archived. No new replies allowed.