
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a C Program to delete the duplicate numbers in an array
Let the user enter the numbers in an array, which contains duplicate elements.
Now, let’s write a code to delete the repeated numbers or elements in an array and make an array with unique elements without duplicates
For example,
An example is explained below −
- User input is 12, 30, 12, 45, 67, 30.
- Output is 12, 30, 45, 67 (after deleting duplicates).
Program
Following is the C program to delete the duplicate numbers in an array −
#include <stdio.h> #define MAX 100 // Maximum size of the array int main(){ int array[MAX]; // Declares an array of size 100 int size; int i, j, k; // Loop variables /* Input size of the array */ printf("enter the size of array : "); scanf("%d", &size); /* Input elements in the array */ printf("Enter elements in an array : "); for(i=0; i<size; i++){ scanf("%d", &array[i]); } /*find the duplicate elements in an array: for(i=0; i<size; i++){ for(j=i+1; j<size; j++){ /* If any duplicate found */ if(array[i] == array[j]){ /* Delete the current duplicate element */ for(k=j; k<size; k++){ array[k] = array[k + 1]; } /* Decrement size after removing duplicate element */ size--; /* If shifting of elements occur then don't increment j */ j--; } } } printf("
Array elements after deleting duplicates : ");/*print an array after deleting the duplicate elements. for(i=0; i<size; i++){ printf("%d\t", array[i]); } return 0; }
Output
The output is as follows −
enter the size of array : 10 Enter elements in an array : 23 12 34 56 23 12 56 78 45 56 Array elements after deleting duplicates : 23 12 34 56 78 45
- Related Articles
- C Program to delete the duplicate elements in an array
- C# program to find if an array contains duplicate
- Write a Golang program to find duplicate elements in a given array
- C program to delete an array element using Pointers
- C# program to find all duplicate elements in an integer array
- C# Program to write an array to a file
- Write a program to Delete a Tree in C programming
- Java program to remove the duplicate element in an array
- Write a program to reverse an array or string in C++
- Distance between 2 duplicate numbers in an array JavaScript
- Java program to delete duplicate lines in text file
- Python program to print the duplicate elements of an array
- Java program to delete duplicate characters from a given String
- Write a program in C++ to remove duplicates from a given array of prime numbers
- C++ Program to delete an item from the array without using the library function

Advertisements