Print sorted distinct elements of array in C language

Given an array of integer elements, the task is to remove duplicate values and print the distinct elements in sorted order.

For example, given an array that stores values {4, 6, 5, 3, 4, 5, 2, 8, 7, 0}, we need to identify unique elements and sort them. The final result should be {0, 2, 3, 4, 5, 6, 7, 8}.

Original Array: 4 6 5 3 4 5 2 8 7 0 Sorted Distinct Elements: 0 2 3 4 5 6 7 8 Duplicates Unique & Sorted

Syntax

void printDistinctElements(int array[], int size);

Example Input and Output

Input: array[] = {4, 6, 5, 3, 4, 5, 2, 8, 7, 0}
Output: 0 2 3 4 5 6 7 8

Algorithm Steps

  • Step 1: Find distinct elements by checking each element against all subsequent elements
  • Step 2: Store unique elements in a separate array
  • Step 3: Sort the distinct elements array using bubble sort
  • Step 4: Print the sorted distinct elements

Example

Here's a complete C program to print sorted distinct elements −

#include <stdio.h>

/* Prints distinct elements of an array */
void printDistinctElements(int array[], int size) {
    int i, j, array1[size], temp, count = 0;
    
    /* Find distinct elements */
    for(i = 0; i < size; i++) {
        for(j = i+1; j < size; j++) {
            if(array[i] == array[j]) {
                /* Duplicate element found */
                break;
            }
        }
        /* If j is equal to size, it means we traversed whole
           array and didn't find a duplicate of array[i] */
        if(j == size) {
            array1[count++] = array[i];
        }
    }
    
    /* Sort the array1 where only the distinct values are stored */
    for (i = 0; i < count-1; i++) {
        for (j = i+1; j < count; j++) {
            if(array1[i] > array1[j]) {
                temp = array1[i];
                array1[i] = array1[j];
                array1[j] = temp;
            }
        }
    }
    
    /* Print sorted distinct elements */
    for (i = 0; i < count; ++i) {
        printf("%d ", array1[i]);
    }
    printf("
"); } int main() { int array[] = {4, 6, 5, 3, 4, 5, 2, 8, 7, 0}; int n = sizeof(array)/sizeof(array[0]); printf("Original array: "); for(int i = 0; i < n; i++) { printf("%d ", array[i]); } printf("
"); printf("Sorted distinct elements: "); printDistinctElements(array, n); return 0; }

Output

Original array: 4 6 5 3 4 5 2 8 7 0 
Sorted distinct elements: 0 2 3 4 5 6 7 8 

How It Works

  1. Duplicate Detection: For each element, check if it appears later in the array. If no duplicate is found, it's unique.
  2. Storage: Store unique elements in a separate array array1.
  3. Sorting: Use bubble sort to arrange distinct elements in ascending order.
  4. Display: Print the final sorted array of unique elements.

Time Complexity

  • Finding Duplicates: O(n²) where n is the array size
  • Sorting: O(k²) where k is the number of distinct elements
  • Overall: O(n²) in the worst case

Conclusion

This approach effectively removes duplicates and sorts elements using nested loops. While simple to understand, it has O(n²) time complexity and can be optimized using hash tables or sorting first then removing duplicates.

Updated on: 2026-03-15T11:59:50+05:30

622 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements