Notification texts go here Contact Us Click here

Program For Remove Duplicates Items In An Array In C Language.

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

        Process    Remove Duplicates Items In An Array . 

When the same number of elements occurs in sorted or unsorted array, then
the elements of the array is called the duplicate elements. And we need to 
delete these duplicate elements or same number from an array to make
the resultant array consists of unique elements.
 
For example, there is an integer type array arr[10] that contains { 5, 8, 3, 3, 5, 9} elements.
 In this array, 3 and 5 occurs two times. Hence, these elements are duplicate elements. 
 So, after deleting the duplicate elements from an array arr[], we get 5, 8, 3,9 elements.
 
  Steps to delete the duplicate elements from unsorted array
 
Step 1: Input the size of an array from the user and store into the size variable.
 
Step 2: Use for loop to read the elements of an array and store in arr[i] variable.
 
Step 3: To get the duplicate elements from an array we need to use two for loops.
        Where the first loop start from 0 to size. And the structure of the loop is:
        for (= 0; i < size; i++).
 
  //Another loop selects each element of the array and compare with the
   corresponding element to get the duplicate elements. And the structure of the inner loop        is: 
   for (= i + 1; j < size; j++) and the code to find the subsequent same element is:
   if (arr[i] == arr[j]).
 
Step 4: If any duplicate element is encountered, delete the duplicate element from an array 
       and the size of array is decrement by 1 such that, size = size - 1.
 
Step 5: After that, print the unique elements of an array and the code is:
 
for (= 0; i < size; i++)  
{  
printf ("%d \t" , arr[i]);  
}  
  1. ➤ Program For Remove Duplicates Items In An Array in C Language.
  2.  
  3. #include<stdio.h>
  4. int main()
  5. {
  6.  
  7.   int arr[50];
  8.   int i,j,k,len;
  9.   // Enter the size of array (Number of elements in array)
  10.   printf("Enter the size Of Array :\n");
  11.   scanf("%d",&len);
  12.   //Enter the all initialize number of elements in array
  13.   printf("Enter %d elements for the array:\n",len);
  14.   for(i=0; i<len ; i++)
  15.     scanf("%d",&arr[i]);
  16.  
  17.   for(i=0; i< len-1 ; i++){
  18.     for(j=i+1; j<len; j++){
  19.  
  20.          if(arr[j] == arr[i]){
  21.              k=j;
  22.              while(k<(len-1))
  23.              {
  24.                  arr[k] = arr[k+1];
  25.                  k++;
  26.              }
  27.              len--;
  28.              j=i;
  29.           }
  30.       }
  31.   }
  32.   // Here results print after removing all the duplicates from Array.
  33.   printf("The array after removing duplicates Elements :\n");
  34.  
  35.   for(i=0; i < len; i++)
  36.     printf(" %d",arr[i]);
  37.  
  38.   return 0;
  39. }
  40.  
  41. ➤ OUTPUT:
  42.  
  43. Enter the size Of Array :
  44. 6
  45. Enter 6 elements for the array:
  46. 34
  47. 45
  48. 56
  49. 34
  50. 78
  51. 89
  52. The array after removing duplicates Elements :
  53.  34 45 56 78 89

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.