2

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 . . .

3
  • 6
    Use a std::vector and its erase with std::unique and std::sort. VLAs are not standard. Commented Nov 24, 2013 at 6:13
  • possible duplicate of C++ Delete Duplicate Entries in a vector Commented Nov 24, 2013 at 6:38
  • 1
    You can't resize a C++ array. Commented Nov 24, 2013 at 7:51

3 Answers 3

3

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);    
Sign up to request clarification or add additional context in comments.

2 Comments

we didn't learn vectors yet. Is there any way of doing this using only arrays?
@user2977810 look at the possible implementation of std::unique, use it after sorting the array. Else ask this question only C tag, implementing in pure C
0

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

4 Comments

i think i'm using it incorrectly. when i use it, this code just seems to replace the repeated number with -1 instead of eliminating it.
@user2977810: Your final array is in B not your initial array(a new array is created in the process).
What do i cout to print the new array?
@user2977810 : I've added the print section to the end of the code
0

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

1 Comment

we didn't learn vectors yet. Is there any way of doing this using only arrays?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.