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;
}
|