C Program to Print all Non Repeated Elements in an Array

This is a C Program to find the number of non repeated elements in an array.

Problem Description

This C Program finds the number of non repeated elements in an array.

Problem Solution

Take input from the user and perform operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to find the number of non repeated elements in an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Find the Number of Non Repeated Elements in an Array
 */
#include <stdio.h>
int main()
{
    int array[50];
    int *ptr;
    int i, j, k, size, n;
 
    printf("\n Enter size of the array: ");
    scanf("%d", &n);
    printf("\n Enter %d elements of an array: ", n);
    for (i = 0; i < n; i++)
    scanf("%d", &array[i]);
    size = n;
    ptr = array;
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            if (i == j)
            {
                continue;
            }
            else if (*(ptr + i) == *(ptr + j))
            {
                k = j;
                size--;
                while (k < size)
                {
                    *(ptr + k) = *(ptr + k + 1);
                    k++;
                }
                j = 0;
            }
        }
    }
    printf("\n The array after removing duplicates is: ");
    for (i = 0; i < size; i++)
    {
        printf(" %d", array[i]);
    }
    return 0;
}
Program Explanation

In this C program, we are reading the size of an array using ‘size’ variable. Using for loop we are entering the coefficient element values to array[i] variable. The nested if else condition statement is used to find the number of non repeated elements in an array. If condition statement is used to check if both the values of ‘i’ and ‘j’ variable are equal if the condition is true then execute if condition statement.

advertisement
advertisement

Otherwise, if the condition is false then execute else-if condition statement and check the value of ‘ptr+i’ pointer variable is equal to the value of ‘ptr+j’ pointer variable. If the condition is true, execute the elseif statement. Using while loop checks the value of ‘k’ variable is less than the value of ‘size’ variable if the condition is true. Display the number of non repeated elements in an array.

Runtime Test Cases
 
$ cc pgm76.c
$ a.out
 
Enter size of the array: 6
 
Enter 6 elements of an array: 12
10
4
10
12
56
 
The array after removing duplicates is:  12 10 4 56

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.