Advertisement
add1ctus

Removing duplicates from array

Nov 6th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int j,k,n,arr[50],result[50],position=0;
  6.  
  7.     scanf("%d",&n); //Reading number of elements
  8.     for(j=0;j<n;j++)
  9.         scanf("%d",&arr[j]); //Reading all elements into arr[]
  10.  
  11.     for(j=0;j<n;j++)
  12.     {
  13.         int found=0; //A variable to keep track if duplicate has been found
  14.         for(k=0;k<j;k++) // Checking all numbers from 0 to our current number
  15.             if(arr[j]==arr[k])
  16.                 found=1;
  17.  
  18.         if(!found)
  19.             result[position++]=arr[j]; //If there are no duplicates of the number, we insert it into array at position and increment position
  20.     }
  21.  
  22.     for(j=0;j<position;j++)
  23.         printf("%d ",result[j]); //Print the result array
  24.  
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement