C Program To Print All Non Repeated Elements In An Array

Suppose we aim to highlight and isolate unique numbers from a complete array of numerical values. How can we tackle this puzzle? Fear not, as we will elucidate this concept through a demonstration using C programming language.

The enchantment will be executed by this C code. A script that scans through an array of information and showcases all distinct elements without any duplicates.

Example:

Example

#include <stdio.h>

 

// Function to find and print non-repeated elements

void printNonRepeated(int a[], int b) {

 // Traverse the array

 for (int i = 0; i < b; i++) {

 int j;

 

 // Check if the current element is repeated on not

 for (j = 0; j < b; j++) {

 if (i != j && a[i] == a[j]) {

 break; // Element is repeated, break the inner loop

 }

 }

 

 // If the inner loop did not break, print the non-repeated element

 if (j == b) {

 printf("%d ", a[i]);

 }

 }

}

 

int main() {

 // Example array

 int c[] = {4, 2, 8, 3, 4, 6, 2, 8};

 int d = sizeof(c) / sizeof(c[0]);

 

 printf("Original Array: ");

 for (int i = 0; i < d; i++) {

 printf("%d ", c[i]);

 }

 

 printf("\nNon-Repeated Elements: ");

 printNonRepeated(c, d);

 

 return 0;

}

Output:

Output

Original Array: 4 2 8 3 4 6 2 8

Non-Repeated Elements: 3 6

Explanation:

printNonRepeated Function:

This function is designed to identify and display non-repeated elements. It employs nested loops to iterate through the array and compare each item with all other elements. The inner loop terminates when a matching element is found. An element is considered non-duplicated if the inner loop completes without interruption, and it is then output to the console.

Main Function:

The primary objective of our program is defined within the main function.

The array named as the example array is initialized, and its length is determined. A display of the original elements within the array is output to the console. Identification of unique elements is achieved by invoking the printNonRepeatedElements function.

Our initial task involves a simple operation: identifying and displaying non-repeating elements. The primary function, printNonRepeatedElements, assumes responsibility for this task by iterating through each element in the array and posing a basic query: Are you the only one of your kind in this array? The algorithm pairs up elements sequentially, ceasing the comparison once a match is found. Subsequently, it showcases those elements that do not have duplicates.

In the primary function, we begin by initializing an example array instance and showcasing its contents. Subsequently, we invoke the main function to identify and print each unique element, demonstrating the exceptional functionality of the software. Executing the program using a sample array such as {4, 2, 8, 3, 4, 6, 2, 8} produces a distinct outcome: the uncommon elements "3 6".

Conclusion:

In summary, this C code not only addresses a specific issue but also showcases the elegance of logical thinking in programming. These types of programs help enhance programming abilities and foster algorithmic thinking. They mirror the problem-solving approach in coding by navigating through arrays to identify non-unique items. This implementation highlights the elegance and effectiveness of C, particularly for experienced developers.

Input Required

This code uses input(). Please provide values below: