Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

Patterns

Excersises


Remove duplicate values in a given array in c program


Write a program to remove the duplicate values and print unique values in a given array in c program.

Example : pgm.c
#include<stdio.h>
int main(){
    int n=10,k=0,i,j,h,ch;
    int a[10]={5,1,5,6,7,8,5,4,1,4};
    int dup[100];
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            if(a[i]==a[j]){
                ch=0;
                for(h=0;h<k;h++){
                    if(dup[h]==j){
                        ch=1;
                        break;
                    }
                }
                if(ch==0){
                    dup[k]=j;
                    k++;
                }
            }
        }
    }
    printf("\nUnique Values are ");
    for(i=0;i<n;i++){
        ch=0;
        for(h=0;h<k;h++){
            if(dup[h]==i){
                ch=1;
                break;
            }
        }
        if(ch==0){
           printf("\n%d",a[i]);
        }
    }
    return 0;
}

Output:

Unique Values are
5
1
6
7
8
4