write a program to remove duplicate elements in an array in c
Mohammed
Guys, does anyone know the answer?
get write a program to remove duplicate elements in an array in c from screen.
Program to remove Duplicate Elements in an Array
Program to remove duplicate elements in an array in C language with output and solution.
Simple Program to remove Duplicate Element in an Array
In this tutorial, we will learn how to remove a duplicate element from an array. Before moving forward with the program, if you are not familiar with what is an Array please read this article: Array in C language.
Remove duplicates from the sorted array:
Here we are implementing the program to remove a duplicate element from a sorted array. We will create a temporary array and copy elements from one array to another only in case when there is no next matching element.
Note: This program will work only for sorted array so while providing an input make sure that the given array is in a sorted array, otherwise it will give unexpected outputs.#includeint remove_duplicate(int arr[], int n)
{
if (n == 0 || n == 1)
return n; int temp[n]; int j = 0; int i;
for (i = 0; i < n - 1; i++)
if (arr[i] != arr[i + 1])
temp[j++] = arr[i];
temp[j++] = arr[n - 1];
for (i = 0; i < j; i++)
arr[i] = temp[i]; return j; } int main() { int n; scanf("%d", &n); int arr[n]; int i;
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\nArray Before Removing Duplicates: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
n = remove_duplicate(arr, n);
printf("\nArray After Removing Duplicates: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0; } 10 1 2 2 3 4 5 6 7 7 8
Array Before Removing Duplicates: 1 2 2 3 4 5 6 7 7 8
Array After Removing Duplicates: 1 2 3 4 5 6 7 8
Remove duplicates from the unsorted array:
In this program, we will take extra space to store a new array without the reputation of duplicate elements. We will run a nested loop first one is to copy the element from the array to temp array if that element is existing already in temp variable then we will break the inner loop and continue executing for remaining elements the same process.
#includeint main() { int n, count = 0; scanf("%d", &n);
int arr[n], temp[n];
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; } 10 1 2 2 3 4 5 6 7 7 8
Array Before Removing Duplicates: 1 2 2 3 4 5 6 7 7 8
Array After Removing Duplicates: 1 2 3 4 5 6 7 8
Suggested Tutorials:Arrays in C Loops in C Functions in C
File Input/Output in C
← Prev Next →
C Program to delete the duplicate elements in an array
C Program to delete the duplicate elements in an array - Try to delete the same numbers present in an array. The resultant array consists of unique elements.The ...
C Program to delete the duplicate elements in an array
CServer Side ProgrammingProgramming
C In Depth: The Complete C Programming Guide For Beginners
45 Lectures 4.5 hours
TELCOMA Global More Detail
Practical C++: Learn C++ Basics Step By Step
50 Lectures 4.5 hours
Edouard Renard More Detail
Master C And Embedded C Programming- Learn As You Go
66 Lectures 5.5 hours
NerdyElectronics More Detail
Try to delete the same numbers present in an array. The resultant array consists of unique elements.
The logic to delete the duplicate elements in an array is as follows −
for(i=0;ifor(j = i+1; j < number; j++){
for(k = j; kif(a[i] == a[j]){
a[k] = a[k+1]; } j--; number--; } } }
The logic to display the numbers after deleting the duplicates is as follows −
for(i=0;iprintf("%d ",a[i]); }
Program
Following is the C program to delete the duplicate elements in an array.
#includeLive Demo
#includeint main(){
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array
");
scanf("%d",&number);
printf("Enter Elements of the array:
for(i=0;i");
scanf("%d",&a[i]); dup[i] = -1; }
printf("Entered element are:
for(i=0;i");
for(i=0;iprintf("%d ",a[i]); }
for(j = i+1; j < number; j++){
for(k = j; kif(a[i] == a[j]){
a[k] = a[k+1]; } j--; number--; } } } printf("
After deleting the duplicate element the Array is:
for(i=0;i");
printf("%d ",a[i]); } }
Output
When the above program is executed, it produces the following result −
Enter size of the array
10
Enter Elements of the array:
1 1 2 4 3 5 6 5 7 1
Entered element are:
1 1 2 4 3 5 6 5 7 1
After deleting the duplicate element, the Array is:
1 2 4 3 5 6 7 Bhanu Priya
6 Followers
Follow
Updated on 24-Mar-2021 14:23:28
2K+ Views 0Print Article
Related Articles
Write a C Program to delete the duplicate numbers in an array
C# program to find all duplicate elements in an integer array
Python program to print the duplicate elements of an array
How to delete elements from an array?
Program to find duplicate elements and delete last occurrence of them in Python
C# program to find if an array contains duplicate
C program to delete an array element using Pointers
How to duplicate elements of an array in the same array with JavaScript?
Removing duplicate elements from an array in PHP
How to remove duplicate elements of an array in java?
How to remove duplicate elements from an array in JavaScript?
C program to reverse an array elements
Java program to remove the duplicate element in an array
C program to find the unique elements in an array.
C++ program to find array after removal of left occurrences of duplicate elements
Previous Page Next Page Advertisements
स्रोत : www.tutorialspoint.com
Remove Duplicate Elements from an Array in C
Remove Duplicate Elements from an Array in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c union, c strings etc.
Remove Duplicate Elements from an Array in C
This section will discuss the removing or deletion of the duplicate elements from an array in the C programming language. When the same number of elements occurs in sorted or unsorted array, then the elements of the array is called the duplicate elements. And we need to delete these duplicate elements or same number from an array to make the resultant array consists of unique elements.
For example, there is an integer type array arr[10] that contains { 5, 8, 3, 3, 5, 9} elements. In this array, 3 and 5 occurs two times. Hence, these elements are duplicate elements. So, after deleting the duplicate elements from an array arr[], we get 5, 8, 3,9 elements.
Steps to delete the duplicate elements from unsorted arrayStep 1: Input the size of an array from the user and store into the size variable.
Step 2: Use for loop to read the elements of an array and store in arr[i] variable.
Step 3: To get the duplicate elements from an array we need to use two for loops. Where the first loop start from 0 to size. And the structure of the loop is: for (i = 0; i < size; i++).
Another loop selects each element of the array and compare with the corresponding element to get the duplicate elements. And the structure of the inner loop is: for (j = i + 1; j < size; j++) and the code to find the subsequent same element is: if (arr[i] == arr[j]).
Step 4: If any duplicate element is encountered, delete the duplicate element from an array and the size of array is decrement by 1 such that, size = size - 1.
Step 5: After that, print the unique elements of an array and the code is:
for (i = 0; i < size; i++)
{
printf ("%d \t" , arr[i]);
}
Example 1: Program to remove the duplicate elements from an arrayLet's consider an example to delete the same number or duplicate elements from an array in the C programming language.
#include #includeint main () {
// declare local variables
int arr[20], i, j, k, size;
printf (" Define the number of elements in an array: ");
scanf (" %d", &size);
printf (" \n Enter %d elements of an array: \n ", size);
// use for loop to enter the elements one by one in an array
for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);
}
// use nested for loop to find the duplicate elements in array
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
// use if statement to check duplicate element
if ( arr[i] == arr[j])
{
// delete the current position of the duplicate element
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
// decrease the size of array after removing duplicate element
size--;
// if the position of the elements is changes, don't increase the index j
j--; } } }
/* display an array after deletion or removing of the duplicate elements */
printf (" \n Array elements after deletion of the duplicate elements: ");
// for loop to print the array
for ( i = 0; i < size; i++)
{
printf (" %d \t", arr[i]);
} return 0; }
When we execute the above program in C compiler, it produces the given below output in the console screen.Define the number of elements in an array: 10
Enter 10 elements of an array:
57 12 89 32 62 12 89 35 67 75
Array elements after deletion of the duplicate elements: 57 12 89 32 62 35 67 75
Remove the Duplicate Elements from Sorted ArraySuppose we have a given sorted array and the task is to remove the same number of elements from the array. For example, there is an integer type array whose size is 5 and contains arr[] = {2, 2, 3, 3, 5} elements. In this sorted array, 2 and 3 numbers occurs two times. Hence, these elements are duplicate elements. So, after deleting the duplicate elements from an array arr[], we get 2, 3, 5 elements and the new size of array is 3.
Algorithm to delete the duplicate elements from sorted arrayFollowing is the algorithm to delete the duplicate array's elements from the sorted array in the C programming language.
Define the size of elements of the array.
Read the array elements from the user.
Repeat from i = 1 to num.
if (arr[i] != arr [i + 1]
temp [j++] = arr[i]
temp [j++] = arr[n- 1]
Repeat from i = 1 to j
arr[i] = temp[i] arr [i] = temp [i] Return j.
Print unique elements of the array.
Example 2: Program to remove the duplicate elements from sorted array using the user-defined functionLet's consider an example to delete the same number or duplicate elements from sorted array in the C programming language.
/* program to delete the duplicate elements from sorted array in C. */
#includeint duplicate_element ( int arr[], int num)
{
// check num is equal to 0 and num == 1
if (num == 0 || num == 1)
return num;
// create temp array to store same number
int temp [num]; // declare variable int i, j = 0;
// use for loop to check duplicate element
for (i = 0; i < num - 1; i++)
Guys, does anyone know the answer?