Duplicate Number Removal Array

Hey guys, I am having trouble finishing this program. It does not work. I am suppose to have the user input 10 numbers and if there are duplicates it will only show once.
For example:
User input: 1 2 2 4 5 5
Output: 1 2 4 5

So far I have this
#include<iostream>
#include<iomanip>
using namespace std;

//Main function
int main()
{
//Holds variable for length
const int length = 11;
//Holds variables for length and array
int dedup[length];
int array[10];

cout <<"Please enter 10 integers, hitting return after each one: ";
//Sets i,i2,i3 to 0
int i=0;
int i2=0;
int i3=0;

//Loop for user input of 10 numbers
for(i=0; i < 10; i++)


cin >> array[i];

for(i2=0; i2 < 10; i++)
{
if(array[i] == 0)
array[i] = 200;
if(array[i] == dedup[i2])
{
break;
}
else if(i == 9)
{
dedup[i3] = array[i];
i3++;
}
}
return 0;
}

//Funtion to find duplicates
void output(int dedup[], int i3);
{
int count = 0;
cout <<"You entered"<<i3<<"distinct numbers.";

for(; count < i3; count++)
{
if(dedup[count] == 200)
{
dedup[count] = 0;
}
cout << dedup[count]<<" ";
cout << endl;
}
}
Last edited on
We've been getting a lot of duplicate number questions on this forum lately, identical to that one.

for(i2=0; i2 < 10; i++)
What is this?

-Albatross
Last edited on
that was what the tutor told me to write but he had to leave before i was finished.
@b33p
I think you need to check that line more carefully. There is a big mistake in it.
Hey here's my answer to that!

Some comment before though: Next it's not the best coded programme or most efficient or whatever--- what I wanted to do was introduce new approach since it's a C++ forum. What i mean by that was C++ not C/C++. So what's the difference between:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main() //: main()  C compiler.
{
   printf("Hello world!\n");
   return 0;
}

 To this other one?

#include <iostream>

int main()
{
   std::cout << "Hello world! << std::end;
   return 0;
} 


Just diferent libraries, but what about this one?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

class HelloWorld
{
   public:
       HelloWorld() { std::cout << "Hello world!" << std::end; }
};

int main()
{
   HelloWorld hello;
   return 0;
}


Ok, some might say that C++ is not only OOP or that's not for a beginner level and so on... but I'm a proponent that the sooner u get to understand this OPP approach the easiest it's gonna be learn to program in C++.I mean to fully use its potencial... well enough already.

Repeated number removal:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

class Array
{
public:
	Array(char const * prompt): r_cnt(0)
	{
		std::cout << prompt << max <<": \n";// can be improved!!!
		for(int i(0); i < max; ++i)
		{
			std::cin >> vect[i];
			if( i != 0 && is_repeated(vect[i], i) )
			{
				rep_vect[r_cnt++] = vect[i];
				
				/*for(int k(0); k < i; ++k)
				{
					if(vect[k] == vect[i])
					{
						rep_vect[r_cnt] = vect[k];
						r_cnt++;
					}
				}*/
			}
		}
	}
	void Display()
	{
		for(int i(0); i < max; ++i)
			if(i == 0)
			std::cout << vect[i] << ' ';
			else if( !is_repeated(vect[i], i) )
				std::cout << vect[i] << ' ';
		
		std::cout << "\n\nThese are the repeated\n";
		
		for(int i(0); i < r_cnt; ++i)
			std::cout << rep_vect[i] << ' ';
	}
	
private:
	enum {max = 10};
	int vect[max];
	int rep_vect[max/2];// how many time can u repeat an element in N element vector?
	int r_cnt;
	bool is_repeated(int num, int id)	
	{
		bool found(false);// local found.
		for(int i(0); i < id && !found; ++i) //: while not found iterates
		{
			if(vect[i] == num)
				found = true;
		}
		if(found) return true; 
		return false;// notice i dont return found; Not a good idea to return local variables.
	}
		
};


int main()
{
	Array arr("Enter integers hitting return after each one; Only: ");
	arr.Display();
}


Look i didn't remove the repeated numbers just decided to print the non-repeated and store repeated ones in another vector. But if what you wanted was to remove, which is hard in a staticly allocated vector, one thing to do is to REPLACE it for some maybe negative number;

1
2
3
4
5
6
7
8
9
                for(int i(0); i < max; ++i)
		{
			std::cin >> vect[i];
			if( i != 0 && is_repeated(vect[i], i) )
			{
				vect[i] = -1;
                        }
                  }
				


Then u just print the positives... Anothe way around is to create even another vector to store them.

Again, maybe I'm not being of much help if u wanted to get your code corrected but i still hope at least to have brought in some new insight! Study the functions in the class try to minimize the code or even find other ways around it.
Oi OI OI! That looks a LOT like a full and vastly overblown solution. It may or may not work, but we tend to dislike both on these forums.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

class HelloWorld
{
   public:
       HelloWorld() { std::cout << "Hello world!" << std::end; }
};

int main()
{
   HelloWorld hello;
   return 0;
}


That code... is... overblown.

-Albatross
Last edited on
Hey here's my answer to that!

Some comment before thought: Next it's not the best coded programme or most efficient or whatever--- what I wanted to do was introduce new approach since it's a C++ forum what i mean by that was C++ not C/C++. So what's the difference between:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main() //: main()  C compiler.
{
   printf("Hello world!\n");
   return 0;
}

 To this other one?

#include <iostream>

int main()
{
   std::cout << "Hello world! << std::end;
   return 0;
} 
 


Just diferent libraries, but what about this one?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

class HelloWorld
{
   public:
       HelloWorld() { std::cout << "Hello world!" << std::end; }
};

int main()
{
   HelloWorld hello;
   return 0;
}





Ok, some might say that C++ is not only OOP or that's not for a beginner level and so on... but I'm a proponent that the sooner u get to understand this OPP approach the easiest it's gonna be learn to program in C++.I mean to fully use its potencial... well enough already.

Repeated number removal:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

class Array
{
public:
	Array(char const * prompt): r_cnt(0)
	{
		std::cout << prompt << max <<": \n";// can be improved!!!
		for(int i(0); i < max; ++i)
		{
			std::cin >> vect[i];
			if( i != 0 && is_repeated(vect[i], i) )
			{
				rep_vect[r_cnt++] = vect[i];
				
				/*for(int k(0); k < i; ++k)
				{
					if(vect[k] == vect[i])
					{
						rep_vect[r_cnt] = vect[k];
						r_cnt++;
					}
				}*/
			}
		}
	}
	void Display()
	{
		for(int i(0); i < max; ++i)
			if(i == 0)
			std::cout << vect[i] << ' ';
			else if( !is_repeated(vect[i], i) )
				std::cout << vect[i] << ' ';
		
		std::cout << "\n\nThese are the repeated\n";
		
		for(int i(0); i < r_cnt; ++i)
			std::cout << rep_vect[i] << ' ';
	}
	
private:
	enum {max = 10};
	int vect[max];
	int rep_vect[max/2];// how many time can u repeat an element in N element vector?
	int r_cnt;
	bool is_repeated(int num, int id)	
	{
		bool found(false);// local found.
		for(int i(0); i < id && !found; ++i) //: while not found iterates
		{
			if(vect[i] == num)
				found = true;
		}
		if(found) return true; 
		return false;// notice i dont return found; Not a good idea to return local variables.
	}
		
};


int main()
{
	Array arr("Enter integers hitting return after each one; Only: ");
	arr.Display();
}




Look i didn't remove the repeated numbers just decided to print the non-repeated and store repeated ones in another vector. But if what you wanted was to remove, which is hard in a staticly allocated vector, one thing to do is to REPLACE it for some maybe negative number;



1
2
3
4
5
6
7
8
9
10
11
12
	

                for(int i(0); i < max; ++i)
		{
			std::cin >> vect[i];
			if( i != 0 && is_repeated(vect[i], i) )
			{
				vect[i] = -1;
                        }
                  }
				



Then u just print the positives... Anothe way around is to create even another vector to store them.

Again, maybe I'm not being of much help if u wanted to get your code corrected but i still hope at least to have brought in some new insight! Study the functions in the class try to minimize the code or even find other ways around it.
Duplicate posts FTL.

Your first question you answered yourself. The second question creates an object stored in your memory which upon construction says "Hello World!". You don't need to create a container for a function to use the function. Classes are actually useful when you want data that you can manipulate using its included functions.
http://cplusplus.com/doc/tutorial/structures/
http://cplusplus.com/doc/tutorial/classes/

There is no std::end.

Never post full solutions to any problem on this forum.

-Albatross
Last edited on
Yeah...OOP (at least in C++) is for actual data, not for obfuscating how the code works.
Wow!

1- Didn't mean to hurt any feeling or touch any sensitivity!

2-"There is no std::end": it's just a typo and it seems to that u knew that.

3-I dont think the person that posted with the problem will ever hand in a programme like mine. The programme is complete to try it. At least i just wanted to throw in new insight, maybe by studying the functions or anything or getting any idea, some helps.

4-My mistake i dont know how to use the forum!

5-We are all beginners, aint we? I wont play into you for some code layout I would try to help KINDLY!!!!




Erm... Demien... um... we have no guarantees that the OP will not turn in a program identical to yours in any way on this forum. That's... why we have that "rule". No offense to the OP intended. Instead, give the general idea without the implementation. That's what most of us do here.

Oh, and we provide links.

-Albatross
Topic archived. No new replies allowed.