Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
25. Write a program to delete duplicate element in a list of 10 elements & display it on screen.
/*
File: Prgrm25.c
Author: Aditya Saini
Date: Jan 17, 2021
Description: Program to delete duplicate element in a list & display it on screen.
*/
#include <stdio.h>
int main (void)
{
int list[10], duplicate[10];
int i, j, k, m;
//Input list
for (i = 0; i <= 9; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &list[i]);
}
//Print original list
printf ("Original List: ");
for (i = 0; i <= 9; i++)
printf ("%d ", list[i]);
printf ("\n");
//Find and delete duplicate elements
m = 0;
for (i = 0; i <= 8 - m; i++)
for (j = i + 1; j <= 9 - m; j++)
{
if (list[i] == list[j])
{
duplicate[m] = list[j];
for (k = j; k <= 8 - m; k++)
list[k] = list[k + 1];
m++;
}
}
//Print duplicate elements
printf ("Duplicate elements: ");
for (i = 0; i <= m - 1; i++)
printf ("%d ", duplicate[i]);
printf ("\n");
//Print new list
printf ("New List: ");
for (i = 0; i <= 9 - m; i++)
printf ("%d ", list[i]);
};
Output
Input 1 element: 56
Input 2 element: 24
Input 3 element: 94
Input 4 element: 27
Input 5 element: 56
Input 6 element: 98
Input 7 element: 56
Input 8 element: 27
Input 9 element: 40
Input 10 element: 24
Original List: 56 24 94 27 56 98 56 27 40 24
Duplicate elements: 56 56 24 27
New List: 56 24 94 27 98 40
No comments:
Post a Comment
Please do not post spam links.