C Program - Elimination of duplicate numbers using an arrays

#include<stdio.h>

int main()
{
// 2 arrays with 10 elements
// counter is initialized to 0
int  array1[10], array2[10], counter = 0;
int j;
// Input 10 numbers
printf("Enter 10 Numbers = ");
for (int i = 0;i<10; i++)
scanf("%d", &array1[i]);

// outer loop runs 10 times
for (j = 0; j<10;j++)
{

int k;
for (k = 0; k<counter; k++)
{
// If the array 1 element is equal to array 2 element it exits the loop
if (array1[j] == array2[k])
break;
}
//else it will add that new element to another array 2 and increment the counter
if (k == counter)
{
array2[counter] = array1[j];
counter++;
}
}
// now counter is the number of elements in array 2
// prints the elements in array 2
printf("Elements after removing duplicate numbers \n");
for (j = 0; j<counter; j++)
printf("%d ", array2[j]);

getch();
}

Comments

Popular posts from this blog

How to Block Websites without using any Software.

C Program - Greater, less and Equal

C Program - Even Odd