C Program to Print Unique Elements in an Array

C Program to Print Unique Elements in an Array

C program to print unique elements in an array; Through this tutorial, we will learn how to print all unique elements in an array using standard methods and functions in c programs.

Programs to Print Unique Elements in an Array in C

  • C Program to Print Unique Elements in an Array using Standard Method
  • C Program to Print Unique Elements in an Array using Function

C Program to Print Unique Elements in an Array using Standard Method

#include <stdio.h>
 
int main()
{
    int a[10000],b[10000],i,j,n,c=0 ;
   
    printf("Enter size of the array : ");
    scanf("%d", &n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    
  for(i=0; i<n; i++)
    {
        c=1;
        if(a[i]!=-1)
		{
		    for(j=i+1; j<n; j++)
     
            {
        	   if(a[i]==a[j])
        	    {
			       c++;
			       a[j]=-1;
		       }
	       }
	       b[i]=c;
		}
         
   
          
    }
     
            printf("unique numbers in the  array :\n");
 
         
 for(i=0; i<n; i++)
    {
         if(a[i]!=-1)
        {
        	if(b[i]==1)
        	printf("%d\n",a[i]);
 
		} 
         
    }    
    return 0;
} 

The output of the above c program; as follows:

Enter size of the array : 5
Enter elements in array : 1 1 2 2 3
unique numbers in the  array :
3

C Program to Print Unique Elements in an Array using Function

#include <stdio.h>
 
count(int *a,int *b,int n)
{ 
    int i,c,j;
    for(i=0; i<n; i++)
    {
        c=1;
        if(a[i]!=-1)
		{
		    for(j=i+1; j<n; j++)
     
            {
        	   if(a[i]==a[j])
        	    {
			       c++;
			       a[j]=-1;
		       }
	       }
	       b[i]=c;
		}
         
   
          
    }
     
 }
 
 print(int *a,int *b,int n)
 { 
    int i;
    
	printf("unique numbers in the  array :\n");
 
    for(i=0; i<n; i++)
    {
         
        if(a[i]!=-1)
        {
        	if(b[i]==1)
        	{
			
        	printf("%d \n",a[i]);
 
		    }   
		}
		 
         
    }
 	
 }
int main()
{
    int a[10000],b[10000],i,n;
   
    printf("Enter size of the array : ");
    scanf("%d", &n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    
    count(a,b,n);
    
	print(a,b,n);   
    
	return 0;
}

The output of the above c program; as follows:

Enter size of the array : 6
Enter elements in array : 1 1 2 2 3 4
unique numbers in the  array :
3 
4 

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *