Ad Code

Print the Duplicate and Unique Elements in an Array.


In this article, we will describe how to print duplicate elements from an array and print unique elements from an array with the help of C, C++, and Java programming languages. 

In these programs, we will use two for loops and an if-else statement.

C code to print the unique elements from an array.


 #include <stdio.h>

 int main()
 {
    int input[]={1,2,3,1,2,6};
    int i, j;
    int n = sizeof(input)/sizeof(input[0]);
    printf("Unique Elements: ");
    
     for(i=0; i<n; i++){
        int flag=0;
        for(j=0; j<i; j++){
            if(input[i]==input[j]){
                flag=1;
                break;
            }
        }
        if(flag==0){
            printf("%d",input[i]);
        } 
    }
    return 0;
 }

Output:

Unique Elements: 1236

C++ code to print the unique elements from an array.


 #include <iostream>
 using namespace std;
 int main()
 {
    int input[]={1,2,3,1,2,6};
    int i, j;
    int n = sizeof(input)/sizeof(input[0]);
    cout<<"Unique Elements: ";
    
     for(i=0; i<n; i++){
        int flag=0;
        for(j=0; j<i; j++){
            if(input[i]==input[j]){
                flag=1;
                break;
            }
        }
        if(flag==0){
            cout<<input[i];
        } 
    }
    return 0;
 }

Output:

Unique Elements: 1236

Java code to print the unique elements from an array.


 public class Main
 {
 	public static void main(String[] args) {
 	    int input[] = {1,2,3,1,2,6};
        int i,j;
        int n = input.length;
        System.out.print("Unique Elements: ");
        for(i=0; i<n; i++){
        int flag=0;
        for(j=0; j<i; j++){
            if(input[i]==input[j]){
                flag=1;
                break;
            }
        }
        if(flag==0){
 		System.out.print(input[i]);
        } 
    }
 	}
 }

Output:

Unique Elements: 1236

Now, we are going to print duplicate elements from an array with the help of C, C++, Java languages.

C code to print the duplicate elements from an array.


 #include <stdio.h>

 int main()
 {
    int input[]={1,2,3,1,2,6};
    int i, j;
    int n = sizeof(input)/sizeof(input[0]);
    printf("Duplicate Elements: ");
    
     for(i=0; i<n; i++){
        int flag=0;
        for(j=0; j<i; j++){
            if(input[i]==input[j]){
                flag=1;
                break;
            }
        }
        if(flag==1){
            printf("%d",input[i]);
        } 
    }
    return 0;
 }

Output:

Duplicate Elements: 12

C++ code to print the duplicate elements from an array.


 #include <iostream>
 using namespace std;
 int main()
 {
    int input[]={1,2,3,1,2,6};
    int i, j;
    int n = sizeof(input)/sizeof(input[0]);
    cout<<"Duplicate Elements: ";
    
     for(i=0; i<n; i++){
        int flag=0;
        for(j=0; j<i; j++){
            if(input[i]==input[j]){
                flag=1;
                break;
            }
        }
        if(flag==1){
            cout<<input[i];
        } 
    }
    return 0;
 }

Output:

Duplicate Elements: 12

Java code to print the duplicate elements from an array.


 public class Main
 {
 	public static void main(String[] args) {
 	    int input[] = {1,2,3,1,2,6};
        int i,j;
        int n = input.length;
        System.out.print("Duplicate Elements: ");
        for(i=0; i<n; i++){
        int flag=0;
        for(j=0; j<i; j++){
            if(input[i]==input[j]){
                flag=1;
                break;
            }
        }
        if(flag==1){
 		System.out.print(input[i]);
        } 
    }
 	}
 }

Output:

Duplicate Elements: 12

Post a Comment

0 Comments

Ad Code