HomeC ProgrammingC Program to Print all Non Repeated Elements in an Array

C Program to Print all Non Repeated Elements in an Array

This C program finds and prints all non-repeated elements in an array. It provides a solution to identify unique elements without repetition.

Program Statement

Program Statement: Print all Non-Repeated Elements in an Array

Write a C program that reads an array of integers and prints all the non-repeated elements present in the array. The program should implement the following:

  1. Prompt the user to enter the size of the array.
  2. Read the array elements from the user.
  3. Define a function printNonRepeatedElements that takes the array and its size as parameters.
  4. Inside the printNonRepeatedElements function, iterate through each element of the array.
  5. For each element, count the number of occurrences by comparing it with all other elements in the array.
  6. If the count is equal to 1, print the element as a non-repeated element.
  7. In the main function, call the printNonRepeatedElements function with the array and its size.
  8. Print the non-repeated elements obtained from the printNonRepeatedElements function.

Ensure that your program handles different test cases and works correctly for arrays of varying sizes.

C Program to Print all Non Repeated Elements in an Array

#include <stdio.h>

void printNonRepeatedElements(int arr[], int size) {
    // Iterate through each element in the array
    for (int i = 0; i < size; i++) {
        int count = 0;

        // Check if the element is repeated
        for (int j = 0; j < size; j++) {
            if (arr[i] == arr[j])
                count++;
        }

        // If the count is 1, the element is non-repeated
        if (count == 1)
            printf("%d ", arr[i]);
    }
}

int main() {
    int arr[] = {2, 4, 5, 2, 6, 7, 8, 6, 4};
    int size = sizeof(arr) / sizeof(arr[0]);

    printf("Non-repeated elements in the array: ");
    printNonRepeatedElements(arr, size);

    return 0;
}

How it Works?

  1. The program starts by prompting the user to enter the size of the array.
  2. The user enters the size of the array, and the program reads it.
  3. The program then prompts the user to enter the elements of the array.
  4. The user enters the elements of the array, and the program reads them.
  5. The program defines a function called printNonRepeatedElements that takes the array and its size as parameters.
  6. Inside the printNonRepeatedElements function, the program iterates through each element of the array using a loop.
  7. For each element, the program initializes a variable called count to 0. This variable will be used to count the number of occurrences of the current element.
  8. The program then uses another loop to compare the current element with all other elements in the array.
  9. If the current element matches with another element in the array, the count variable is incremented.
  10. After counting the occurrences of the current element, the program checks if the count is equal to 1.
  11. If the count is 1, it means that the current element is non-repeated because it only occurred once in the array.
  12. In such a case, the program prints the current element as a non-repeated element.
  13. Once the printNonRepeatedElements function has iterated through all the elements in the array, it returns to the main function.
  14. In the main function, the program calls the printNonRepeatedElements function with the array and its size as arguments.
  15. The printNonRepeatedElements function processes the array and finds the non-repeated elements.
  16. The non-repeated elements obtained from the printNonRepeatedElements function are printed in the main function.
  17. The program ends.

By following these steps, the program identifies and prints all the non-repeated elements present in the given array.

Input/Output

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...