hussain alwazzan
hussain alwazzan

Reputation: 1

counting duplicates and put the unique entries in to new array in array [c++]

q.Write a program that contains a function named "remove_duplicates" that takes an array of integers in random order, and then eliminates all the duplicate integers in the array. The function should take three arguments:

  1. An array of integers which is (read and filled in main)
  2. The size of the array.
  3. A variable passed by reference to be printed in main to show how many values in the array without duplication. The function should not return a value, but if any duplicate integers are eliminated, then the function should count that, so the new value tells the number of distinct integers in the array. Suppose the array passed to the function is as shown below, and the size of the array passed to the function is 10.

    0 1 2 3 4 5 6 7 8 9 58 | 26 | 91 | 26 | 70 | 70 | 91 | 58 | 58 | 66

The function should change the array to look like this:

0       1       2     3       4      5      6      7     8      9    
58 | 26 | 91 | 70 | 66 | ?? | ?? | ?? | ?? | ??

and it should change the value of the distinct counter so that it is 5. The question marks in the cells after the 5th cell indicate that it does not matter what numbers are in those cells when the function returns.

something is wrong with my function

void remove_duplicates ( int h[] ,int n ,int &count ){
count = 0;
int a[100];
for (int i = 0 ; i < n  ; i++ )
        for(int j = i ; j<n ; j++){
            if(h[i]!=h[j])
                        a[i]=h[i];
            else
            count++;

Upvotes: 0

Views: 1559

Answers (4)

Spidey
Spidey

Reputation: 2589

If you have a defined range or a possible invalid value (say, -1), you can set the duplicates to this invalid value, and upon finding a duplicate while you traverse the array, you shift elements back to the start.

Upvotes: 0

Adina T.
Adina T.

Reputation: 21

Your count will be larger than it should when the function is done, because you increment it more than once for each element. You should try something like this:

    void remove_duplicates ( int h[] ,int n ,int &count ){
count = n;
int a[100],ok,p;
for (int i = 0 ; i < count-1  ; i++ ) {
    ok=1;
    for(int j = i+1 ; j<count ; j++)
        if(h[i]==h[j]) {
            ok=0;
            p=j;
        }
    if(!ok) {
        for(int j = p+1; j<count; j++) 
            h[j-1]=h[j];
        count--;
    }
}
count=n-count;
}

"ok" checks if v[i] can be found somewhere else in the array and p is the position where it appears, so it knows which element to eliminate. I assumed you need to keep the first appearance of an element in the array, otherwise you could have eliminated it easier. Hope it helps.

Upvotes: 0

Anon Mail
Anon Mail

Reputation: 4770

  1. Copy the entire array into a std::set. The set will eliminate the duplicates, meaning insertion of duplicates will fail.

  2. Copy the set back to the array.

  3. The number of unique values is the size of the set.

Upvotes: 1

AlexTheo
AlexTheo

Reputation: 4164

There are many solutions of these problem I will show you one without temp arrays. If your requirements allow you to use the sorting algorithm then sort the array before applying this code:

unsigned int numberOfDuplicates = 0;
for( unsigned int i = 1; i<numberOfElements; i++ )
{
  if( array[i-1] == array[i] ){
    numberOfDuplicates++;
    for( unsigned int j = i; j< numberOfElements-1; j++ )
    {
       array[j] = array[j+1];
    }
  }
}

Each time you find a new duplication you eat it by shifting the rest of the array one position left. A second solution is by allocating a new array and passing there only the values which are not exists already in it after that just copy the allocated array back.

Without sorting the algorithm could be:

unsigned int numberOfDuplicates = 0;
for( unsigned int i = 1; i<numberOfElements; i++ )
{
 for( unsigned int k = i; k < numberOfElements; k++){
   if( array[i-1] == array[k] ){
      numberOfDuplicates++;
      for( unsigned int j = k; j< numberOfElements-1; j++ )
      {
       array[j] = array[j+1];
      }
   }
  }
}

Upvotes: 0

Related Questions