1

Create Function that removes doubled items from an array.(Store it into an array, this array can be accessible from the main program. Use functions and arrays)

Example:

Input: 3 4 4 3 4 1
Output: 4 4 4 1

I made a code to tackle the problem, but its horribly ugly and I honestly don't know how to remove an element/item from an array, so instead I tried to sort into another array, and I'm 100% sure it won't work. Does someone know any insights on how to tackle this kind of problem? If you have any suggestions for cleaner and shorter code, I'd be very happy to hear them out. Thanks so much in advance. :)

#include<stdio.h>
int doubleElement(int* arr,int *filter,int n);


int main()
{

    int i, n;

    printf("Enter no of elements."); //first prompt
    scanf("%d", &n);

    int arr[n];
    int filter[n]; 

    printf("\nEnter Elements:");   
    
    for(i=0;i<n;i++)
    {
        scanf("%d", &arr[i]);
    }

    doubleElement(arr, filter, n); // Pass into double function

    printf("\nOutput:");
    for(i=0;i<n;i++)
    {
        printf("%d ", filter[i]);
    }
}

int doubleElement(int* arr, int* filter, int n)
{
    int i, j, k=0, count, counter;

    int move=0;
    for(i=0;i<n;i++) // First Loop
    {
        count=1; //Frequency tracker, set to 1 because of base.
        for(j=i+1;j<n;j++) //2nd Loop
        {
            if(arr[i]==arr[j])
            {
                count++;
            }
        }
        if(count==1||count>1) //if statement for non doubled items
        {

            for(k=counter;k<count;k++) // We want to repeat the value contained by arr[i] until we meet count and sort the non doubled item.
            {
                filter[k]=arr[i];
            }
            counter=k;
            move++; //Increment for filter array, everytime there is a successful non doubled item
        }
    }
}
risingStark
  • 1,153
  • 10
  • 17
Kristofer
  • 21
  • 2
  • Can you sort the array? Then it's easy to find consecutive duplicates and copy copy only the first to the destination array. – Some programmer dude May 13 '21 at 15:53
  • 1
    you have duplicate question. here the references: https://stackoverflow.com/questions/9613960/removing-duplicates-from-an-array-using-c – Enrique Arevalo May 13 '21 at 16:02
  • I can try sorting the array, and then going through a loop to check the frequencies, but how would I copy only the duplicates that are equivalent to 1 frequency or greater than 1 frequency? – Kristofer May 13 '21 at 16:04
  • https://stackoverflow.com/questions/9613960/removing-duplicates-from-an-array-using-c here a reference – Enrique Arevalo May 13 '21 at 16:04
  • Look at the _Example_ _Input_ and _Output_ - this is not what the alleged _duplicate question_ is about. – Armali May 13 '21 at 16:42
  • @Kristofer - Don't you need the output exactly as in the _Example_? If you do, how can you imagine to achieve this after you sorted the array? – Armali May 13 '21 at 16:45
  • are there any constraints on the order of elements in output? Would "4 1 4 4" be a valid answer? Moreover, is there any limit on set of values in array.. like 1 to 1000 ? – tstanisl May 13 '21 at 19:40

1 Answers1

0

This is not very efficient, but straightforward, and has the advantage that it works as required:

    for (i = 0; i < n; )
    {   int j, k = arr[i], move, count;
        for (count = j = 0; j < n; ++j) count += arr[j] == k;   // count items equal to k
        if (count == 2)
        {   // doubled (occurring exactly twice) - move elements above index i to the left
            for (move = j = i; move < n; ++move) if (arr[move] != k) arr[j++] = arr[move];
            n = j;  // adjust number of elements
        }
        else ++i;
    }
Armali
  • 18,255
  • 14
  • 57
  • 171