Array Question

I have an array question (which we barely covered in class)

Can you point me in the right direction for this:

"Write a function named "eliminate_duplicates" that takes an array of integers in random order and eliminates all the duplicate integers in the array. The function should take two arguments:
(1) an array of integers;
(2) an integer that tells the number of cells in the array.
The function should return the number of distinct integers in the array.
"
I'm not sure where to start to get a function to eliminate arrays...
I'm guessing the array will need to pull and compare each number to another number in the array... but I'm not quite sure how to accomplish this.

This would be much easier using a vector, ie. http://www.cplusplus.com/reference/algorithm/unique/. However, since you have stated arrays you could do something like this:

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

#include <iostream>
using namespace std;

int eliminateDuplicates(int array[], int size);

int main()
{
	const int MAX_SIZE = 10;

	int numSeq[MAX_SIZE] = { 1, 3, 5, 7, 9, 10, 7, 12, 13, 7};
	int result;

	result = eliminateDuplicates(numSeq, MAX_SIZE);
	cout << endl << "Result is " << result << " numbers." << endl;

	return 0;
}

int eliminateDuplicates(int array[], int size)
{
	// temporary buffer
	int *dataBuffer = new int[size];
	int dataCount = 0;

	bool found = false;

	// make sure the buffer is cleared to 0's
	memset(dataBuffer, 0, sizeof(int)*size);

	// loop through the array 1 by 1
	for (int i = 0; i < size; i++)
	{
		// loop and check if the value
		// is already in our buffer, if
		// not then we add it.
		for (int j = 0; j < size; j++)
			if (array[i] == dataBuffer[j])
				found = true;
		// was it already there?
		if (!found) {
			dataBuffer[dataCount] = array[i];  // nope, add it
			dataCount++;   // increase counter;
		}
		found = false;  // reset flag
	}

	// for debugging, displays the contents of
	// our buffer showing array minus duplicates.
	for (int i = 0; i < dataCount; i++)
		cout << dataBuffer[i] << " ";

	// free the allocated memory
	delete[] dataBuffer;

	return dataCount;
}


For reference, you can get detailed information on Arrays here: http://www.cplusplus.com/doc/tutorial/arrays/

I have dynamically allocated a array in the above example, information on this can be found here: http://www.cplusplus.com/doc/tutorial/dynamic/

Hope this helps :)
Topic archived. No new replies allowed.