removing duplicates in array

I need help on finding what is wrong with my code. Our directions is to remove duplicates in an array. Our professor gave us a variety of examples to test our code. However, my code is not doing it right.
these are the examples our professor gave us to test our code:
{ },
{ 1 },
{ 1, 1 },
{ 1, 1, 2 },
{ 1, 2, 1 },
{ 1, 2, 2 },
{ 1, 1, 1, 2, 1 },
{ 1, 2, 2, 1, 2, 2, 2 },
{ 2, 3, 5, 1, 2, 3, 5, 2, 4, 5, 6, 7, 3, 2, 5, 7, 3, 7, 4, 6 }

and this is the output I get when I run my code:

Array 1: (empty)
No duplicates to remove.

Array 2: 1
After deleting duplicates: 1

Array 3: 1 1
After deleting duplicates: 1 -33686019

Array 4: 1 1 2
After deleting duplicates: 1 2 2

Array 5: 1 2 1
After deleting duplicates: 1 2 -33686019

Array 6: 1 2 2
After deleting duplicates: 1 2 -33686019

Array 7: 1 1 1 2 1
After deleting duplicates: 1 1 1 1 1

Array 8: 1 2 2 1 2 2 2
After deleting duplicates: 1 2 2 2 2 2 2

Array 9: 2 3 5 1 2 3 5 2 4 5 6 7 3 2 5 7 3 7 4 6
After deleting duplicates: 2 3 5 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

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
int deleteDuplicates(int a[], int numOfElems)
{
	for (int i = 0; i < numOfElems; ++i)
	{
		for (int j = i + 1; j < numOfElems; ++j)
		{
			if (a[i] == a[j])
			{
				if (numOfElems >= 2)
				{
					for (int r = j; r < numOfElems; ++r)
					{
						a[r] = a[j + 1];
					}
					--numOfElems;
					--j;
				}

			}
		}
	}

	return 0;
}

// Definition function printArray
void printArray(int a[], int numOfElem)
{
	for (int i = 0; i < numOfElem; i++)
	{
		cout << a[i] << " ";
	}
}
Last edited on
> int deleteDuplicates(int a[], int numOfElems)
Well it looks like you need
int deleteDuplicates(int a[], int &numOfElems)

You're modifying numOfElems in the function, but the caller doesn't see what that new number becomes.
Thanks I got it to work!
Topic archived. No new replies allowed.