Learn Computer Science programming - C# and C++ Algorithms

 

Compact Array

Compact an array by removing any duplicate entries in it.

Code
compactarray.zip
File Size: 4588 kb
File Type: zip
Download File

#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;

int CompactArray(int p[], int s)
{
int current=0, insert=0;
p[insert++] = p[current++];
for (;current <= s; current++)
{
if (p[current] != p[insert-1])
p[insert++] = p[current];
}
return insert-1;
}

void main()
{
int x;
wcout <<  endl << "*** Compact array removing duplicates - Original Array has a size of 20 ***" << endl;
wcout << "*************************************************" << endl;
int g[]={1,2,3,4,4,4,4,5,5,6,7,8,9,9,9,9,9,9,9,10};
int Size = CompactArray(g,sizeof(g)/4); //Dividing the value from sizeof which is 80 by 4 (size of integer)
cout << "New Array size is: " << Size;
cin >> x;
 }