In: Computer Science
in C programming language
Write a function removeDups that removes all duplicates in a given array of type int.
Sample Test Case:
More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
input code:
output:
code:
#include <stdio.h>
int *removeDups(int *arr,int n)
{
/*declare the variables*/
int i,j,k,m;
/*removeDups and make it 0*/
for(i=0;i<n-1;i++)
{
int flag=0;
for(j=i+1;j<n;j++)
{
/*same than make 0*/
if(arr[i]==arr[j])
{
arr[j]=0;
}
}
}
/*add 0 into last*/
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
/*if zero */
if(arr[j]==0)
{
/*than swap to last*/
for(k=j;k<n;k++)
{
arr[k]=arr[k+1];
}
arr[n-1]=0;
}
}
}
return arr;
}
int main()
{
/*declare the variables*/
int i;
int a[]={1,2,2,2,2,3,4,2,4,5,6,6};
/*call the function*/
int *arr1=removeDups(a,12);
/*print std::array<T, N> ;*/
for(i=0;i<12;i++)
{
printf("%d ",arr1[i]);
}
return 0;
}