Create removing_duplicates_from_array.c

Adding C file for removing duplicates form a Sorted array
pull/759/head
TusharKrSoniTKS 2022-05-19 11:44:14 +05:30 committed by GitHub
parent 25e68800e4
commit fb5f62cbaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,58 @@
//This code removes duplicates from a sorted array
//For example we enter the array -:5 6 6 7 8 8
//The the output will be -: 5 6 7 8
#include <stdio.h>
void removeDuplicates(int arr[], int size){
int newsize = size;//keeping track of array size
for(int i=0,j=i+1;j<size;j++){
if(arr[i]==arr[j]){
newsize--;
}else{
i++;
arr[i]=arr[j];
}
}
printf("Array after removing the duplicate elements\n");
for(int i=0;i<newsize;i++){
printf("%d \n",arr[i]);
}
}
//Driver Code
int main()
{
int size;
printf("Enter size of the Array \n");
scanf("%d",&size);
int arr[size];
printf("Enter the sorted Array \n");
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
//checking if input Array is sorted or not
if(i!=0){
if(arr[i]<arr[i-1]){
printf("Array is not sorted");
return 0;
}
else{
printf("Sorted Array is entered");
}
}
}
removeDuplicates(arr,size);
}