Help Removing Duplicates

Hey Guys, I saw examples on the site for removing duplicates within an Array but when I use my code I can't get my inner-for-loop to check the last value in the array and so the array holding 5 7 2 5 1 turns into 5 7 2 1 1 instead of 5 7 2 1.

Please help :)

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>
using namespace std;


//Constants
const int SIZE = 5;

//functions 
void arrFiller(int orgArr[], int SIZE);
//Fills the Array with user Numbers 




int main(){

	int userArr[SIZE];
	arrFiller(userArr, SIZE);

	int numCurrEle = SIZE;


	//Itterate through userArr using i = index and increment the index position by 1
	for (int i = 0; i < SIZE; i++)			
	{
		//Itterate through the userArr again using j = i + 1 so if 'i' is equal to 1 then 'j' equals 2 and increment the 
		for (int j = i+1; j < SIZE; j++)	   
		{
			//check to see if the  value in position userArr[i] is equal to the value in userArr[j]
			if (userArr[i] == userArr[j])
			{
			  //Assign the value that's at position userArr[j + 1] to the value that's in userArr[j]
				for (int k = j; k < numCurrEle - 1; k++)
				{
					userArr[k] = userArr[j + 1];
					
				}
			}
			
		}
	}

	for (int i = 0; i < SIZE; i++)
	{
		cout << "\n" << userArr[i];
	}
	
	system("pause");

	return 0;
}

void arrFiller(int orgArr[], int SIZE)
{
	int temp;

	cout << "You must input 10 numbers " << endl;

	for (int index = 0; index < SIZE; index++)
	{
		cout << "\nPlease enter the next number : ";
		cin >> temp;
		orgArr[index] = temp;
	}

}
It's correct only that you need to ignore the last element. You will probably need a non-const variable to keep track of the size so that you can subtract it when you find a duplicate.
ahh figured it out the correct is as follows:

A buddy helped me out and I see that I was not reducing size as well as the j loop

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
67
68
69
#include <iostream>
using namespace std;


//Constants
const int SIZE = 5;

//functions 
void arrFiller(int orgArr[], int SIZE);
//Fills the Array with user Numbers 

int main(){

	int userArr[SIZE];
	arrFiller(userArr, SIZE);

	// int numCurrEle = SIZE;
	int size = SIZE;
	// our size counter must be mutable!

	//Itterate through userArr using i = index and increment the index position by 1
	for (int i = 0; i < size; i++) // size changes when we find an element        
	{
		cout << "\ninsde i-loop  i = " << i << "and size = " << size; 
		//Itterate through the userArr again using j = i + 1 so if 'i' is equal to 1 then 'j' equals 2 and increment the 
		for (int j = i + 1; j < size; j++)
		{
			cout << "\ninside j-loop: " << j << "and size = " << size;
			//check to see if the  value in position userArr[i] is equal to the value in userArr[j]
			if (userArr[i] == userArr[j])
			{
				cout << "\ninside comparision loop " << "i = " << i << " and j = " << j << "and size = " << size;
				//Assign the value that's at position userArr[j + 1] to the value that's in userArr[j]
				for (int k = j; k < size - 1; k++)
				{
					cout << "\ninside shift k-loop and k = " << k << "and size = " << size;
					userArr[k] = userArr[j + 1];
				}
				--size;
				--j;
			}

		}
	}
	/*
	for (int i = 0; i < size; i++)
	{
		cout << "\n" << userArr[i];
	}
	*/
	system("pause");
	return 0;
}

void arrFiller(int orgArr[], int SIZE)
{
	int temp;

	cout << "You must input 10 numbers " << endl;

	for (int index = 0; index < SIZE; index++)
	{
		cout << "\nPlease enter the next number : ";
		cin >> temp;
		orgArr[index] = temp;
	}

}
Topic archived. No new replies allowed.