Program to Remove duplicate elements from array

0 Suresh Chand June 5, 2021

In this example, We will learn a program to remove duplicate elements from array. Before that, You must have knowledge of the following topics.

Download Application: Learn C Programming with Live Compiler

Algorithm

This is the algorithm of this program

Step 1: Start
Step 2: Declare variables n and count
Step 3: Take input from user and store in n
Step 4: Declare an array with size n as arr[n] and temp[n]
Step 5: Assign i = 0
Step 6: REPEAT this step until i < n
        Take input from user and store in arr[i]
        Increase i by 1
Step 7: Assign i = 0
Step 8: REPEAT this step until i < n
        Declare variable j and assign j = 0
        REPEAT this step until j < count
                IF arr[i] == temp[j]
                        BREAK
                Increase j by 1
        IF j == count
                temp[count] == arr[i];
                Increase count by 1
Step 9: Assign i = 0
Step 10: REPEAT this step until i < count
        PRINT temp[i]
        Increase i by 1
Step 11: End

Flowchart

This is the flowchart of this program

Flowchart : Program to Remove duplicate elements from array

CODE: Program to Remove duplicate elements from array

Now, We will see the code to remove duplicate element in array.

#include <stdio.h>
int main()
{
    int n, count = 0;

    printf("How many elements in array : ");
    scanf("%d", &n);
    int arr[n], temp[n];

    printf("Enter array elements : ");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }

    printf("\nArray Before Removing Duplicates: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    for (int i = 0; i < n; i++)
    {
        int j;
        for (j = 0; j < count; j++)
        {
            if (arr[i] == temp[j])
                break;
        }
        if (j == count)
        {
            temp[count] = arr[i];
            count++;
        }
    }

    printf("\nArray After  Removing Duplicates: ");
    for (int i = 0; i < count; i++)
        printf("%d ", temp[i]);

    return 0;
}

The output of above program is

How many elements in array : 5
Enter array elements : 1
2
3
2
4

Array Before Removing Duplicates: 1 2 3 2 4
Array After  Removing Duplicates: 1 2 3 4

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

Ads

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System