To find non-duplicate numbers in an array

To find non-duplicate numbers in an array

This program can print all non-duplicate numbers present in an array.

Logic:

  • Check the duplicate element in the array.
  • Then eliminate the duplicate elements from the array.
  • Remaining elements are non-duplicate elements.

Algorithm:

  1. Get the input array from the user.
  2. Then check the current element with all other elements.
  3. If the array element reaches the last index and all elements are not equal it non-duplicate element.
  4. Repeat step 2 to step 4 until it reaches the last element.

 

Program:

#include<stdio.h>
void main()
{
   int a[100],i,j,n;
   clrscr();
   printf("enter the length of array");
   scanf("%d",&n);
   printf("enter the elements into array");
   for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   printf("Non duplicate elements are:");
   for(i=0;i<n;i++)
   {
     for(j=0;j<n;j++)
     {
        if(i!=j&&a[i]==a[j])//checks with all elements in array
        {
          break;//if their values are equal it exits from inner loop
        }
        if(j==n-1)//prints only the values not equal and also it reaches last index
        {
           printf("%3d",a[i]);
        }
     }
  }
  getch();
}

 

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.