Removing duplicate values in an array

I need to make a function that removes duplicate values in an array.

What I'm having trouble with right now is I don't know how to make a comparison between every value in the index, as in how do I compare a[0] to a[1], a[2], and so on?

also I don't know how to return each value of the array.

and lastly I have no idea what a reference parameter is, nonetheless update it and shrink the size of the array.

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
/*
Write a function that removes duplicates from an array.For example, if remove_
duplicates is called with an array containing
1 4 9 16 9 7 4 9 11
then the array is changed to
1 4 9 16 7 11
Your function should have a reference parameter for the array size that is updated
when removing the duplicates.
*/

#include "stdafx.h"
#include <iostream>

using namespace std;

int duplicates(int a[], int a_size) {
	for (int i = 0; i > a_size; a++) {
		if (a[i] == a[]) { // I don't know how to get a comparison with every index in the array
			a[i - 1] = a[i];
		}
	}
	return a[]; //not sure how to return every value of the array
}

int main()
{
	int numbers[] = { 1,4,9,16,9,7,4,9,4,11 };
	int nodupes = duplicates(numbers, 10);
	
	cout << nodupes << endl;
	
	system("pause");
    return 0;
}

Last edited on
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
#include <iostream>

using namespace std;

void removeNumber(int numbers[], int idx, int &size)
{
	int i;
	for(i = idx; i < size - 1; i++)
		numbers[i] = numbers[i + 1];
	size--;
}

void removeDuplicate(int numbers[], int &size)
{
	int i, j;
	int number;
	for(i = 0; i < size; i++)
	{
		number = numbers[i];
		for(j = i + 1; j < size; j++)
		{
			if(number == numbers[j])
			{
				removeNumber(numbers, j, size); j--;			
			}
		}
	}
}

int main()
{
	int i;
	int numbers[] = { 1,4,9,16,9,7,4,9,4,11 };

	int size = 10;
	removeDuplicate(numbers, size);
	
	for(i = 0; i < size; i++)
	cout << numbers[i] << ' '; cout << endl;
	
	system("pause");
    return 0;
}


1 4 9 16 7 11

http://cpp.sh/9zyjc
Thank you!
Topic archived. No new replies allowed.