This is a C Program to find the number of non repeated elements in an array.
This C Program finds the number of non repeated elements in an array.
Take input from the user and perform operations as shown in the program below.
Here is source code of the C Program to find the number of non repeated elements in an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Find the Number of Non Repeated Elements in an Array */ #include <stdio.h> int main() { int array[50]; int *ptr; int i, j, k, size, n; printf("\n Enter size of the array: "); scanf("%d", &n); printf("\n Enter %d elements of an array: ", n); for (i = 0; i < n; i++) scanf("%d", &array[i]); size = n; ptr = array; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { if (i == j) { continue; } else if (*(ptr + i) == *(ptr + j)) { k = j; size--; while (k < size) { *(ptr + k) = *(ptr + k + 1); k++; } j = 0; } } } printf("\n The array after removing duplicates is: "); for (i = 0; i < size; i++) { printf(" %d", array[i]); } return 0; }
In this C program, we are reading the size of an array using ‘size’ variable. Using for loop we are entering the coefficient element values to array[i] variable. The nested if else condition statement is used to find the number of non repeated elements in an array. If condition statement is used to check if both the values of ‘i’ and ‘j’ variable are equal if the condition is true then execute if condition statement.
Otherwise, if the condition is false then execute else-if condition statement and check the value of ‘ptr+i’ pointer variable is equal to the value of ‘ptr+j’ pointer variable. If the condition is true, execute the elseif statement. Using while loop checks the value of ‘k’ variable is less than the value of ‘size’ variable if the condition is true. Display the number of non repeated elements in an array.
$ cc pgm76.c $ a.out Enter size of the array: 6 Enter 6 elements of an array: 12 10 4 10 12 56 The array after removing duplicates is: 12 10 4 56
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Apply for C Internship
- Check Computer Science Books
- Practice BCA MCQs
- Check C Books
- Watch Advanced C Programming Videos