how to eliminate the duplicate numbers from the array, and resize the array to the new number of elements in C++

StackOverflow https://stackoverflow.com/questions/20171702

Question

i wrote a program that randomly generates numbers (from 1 to 15) for an integer array of size 17. After filling up the array with these numbers, it sorts the array in a descending order.

Now i want to eliminate the duplicate numbers from the array, and resize the array to the new number of elements. But I don't really know how to do that. Any help would be greatly appreciated. Thank You.

Here's my code:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    const int arraySize = 17;
    int RandomNumbers[arraySize];
    int insert; // temporary variable to hold element to insert

    srand(time(0));

    cout<<"Before Sorting :"<<endl;
    for (int i=0; i<17; i++)
    {
        RandomNumbers[i]= 1+rand()%15;
        cout<<RandomNumbers[i]<<" , ";
    }
    cout<<endl;
    cout<<endl;

    for (int next = 1; next < arraySize; next++)
    {
      insert = RandomNumbers[next]; // store the value in the current element
      int moveItem = next; //initialize location to place element

      //search for the location in which to put the current element
      while ((moveItem >0) &&(RandomNumbers[moveItem -1]< insert) )
      {
        //shift element one slot to the right
          RandomNumbers[moveItem] = RandomNumbers[moveItem -1];
          moveItem--;
      } //end while
      RandomNumbers[moveItem]= insert; //place inserted element into the array
    } //end for

    cout << "After Sorting :"<<endl;

    // output sorted array
    for (int j =0; j < arraySize; j++)
        {cout<<RandomNumbers[j]<<" , ";
        }
    cout<<endl;
    return 0;
}    

Here's my output:

Before Sorting : 13 , 14 , 5 , 12 , 7 , 3 , 9 , 15 , 12 , 3 , 3 , 13 , 13 , 3 , 3 , 10 , 4 ,

After Sorting : 15 , 14 , 13 , 13 , 13 , 12 , 12 , 10 , 9 , 7 , 5 , 4 , 3 , 3 , 3 , 3 , 3 , Press any key to continue . . .

Was it helpful?

Solution 2

Yes, and if you don't want to use the std, this is a code snippet I just wrote. It works ok:

const int length = 5;
int arr[length] = {2,3,2,5,1};

for(int i = 0;i < length;i++)
{
    for(int j = i + 1;j < length;j++)
    {
        if(arr[i] == arr[j] && arr[i] != -1)
            arr[j] = -1;
    }
}

int count = 0;
for(int i = 0;i < length;i++)
{
    if(arr[i] != -1)
        count ++;
}

int *B = new int[count];
int k = 0;
for(int i = 0;i < length;i++)
{
    if(arr[i] != -1)
        B[k++] = arr[i];
}
for(int i = 0;i < count;i++)
{
   cout << B[i] << " ";
}

OTHER TIPS

Simplest form would be using std::vector with std::sort and std::vector::erase+std::unique

#include <algorithm>
#include<vector>
#include<iterator>

template <typename T>
void eliminate_duplicates(std::vector<T>& vec)
{
  std::sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}

template <typename T>
void display(std::vector<T>& vec)
{
  std::copy(vec.begin(),
           vec.end(), 
           std::ostream_iterator<T>(std::cout," ") );
}

std::vector<int> RandomNumbers;
for (int i=0; i<17; i++)
    RandomNumbers.push_back(1+rand()%15);

display(RandomNumbers);

eliminate_duplicates(RandomNumbers);

display(RandomNumbers);    

You could try to use an std::list which will take care of the sizing for you. If its a vector of integers you can have sort for "free" already via std::sort.

std::list

Than just go over the list and delete all duplicates.

std::list<int>::iterator itPrev = myList.begin();
for(std::vector<int>::iterator itcurr = ++itPrev; itCurr != myList.end(); ++itCurr)
{
    if(*itPrev == *itCurr)
    {
        myList.erase(itCurr);
    }
    ++itPrev;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top