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