Home > Backend Development > C++ > Print unique elements in a sorted array in C

Print unique elements in a sorted array in C

王林
Release: 2023-09-20 17:05:01
forward
864 people have browsed it

Given an array of integer elements, the task is to remove duplicate values ​​and print out the different elements in a sorted manner.

Given below is an array that stores integer type values ​​in the order of 4, 6, 5, 3, 4, 5, 2, 8, 7 and 0. Now, the result will be 0, 2, 3 , 4, 4, 5, 5, 6, 7 and 8 print out the sorted elements in the order, but this result still contains duplicate values ​​4 and 5, they should be removed, the final result will be 0, 2, 3, 4, 5, 6, 7 and 8

Print unique elements in a sorted array in C

##Example

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

Explanation

So, to achieve our goal, we will

    Store different elements in another array array1.
  • Sort array1.
  • Print the value of array1.
Algorithm

START
   STEP 1: DECLARE VARIABLES i, j, array1[size], temp, count = 0
   STEP 2: LOOP FOR i = 0 AND i < size AND i++
      LOOP FOR j = i+1 AND j < size AND j++
         IF array[i] == array[j]) then,
            break
         END IF
      END FOR
      IF j == size then,
         ASSIGN array1[count++] WITH array[i]
      END IF
   END FOR
   STEP 3: LOOP FOR i = 0 AND i < count-1 AND i++
      LOOP FOR j = i+1 AND j < count AND j++
         IF array1[i]>array1[j] then,
            SWAP array1[i] AND array[j]
         END IF
      END FOR
   END FOR
   STEP 4: PRINT array1
STOP
Copy after login

Example

#include <stdio.h>
/* Prints distinct elements of an array */
void printDistinctElements(int array[], int size) {
   int i, j, array1[size], temp, count = 0;
   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&#39;t found a duplicate of array[i] */
      if(j == size) {
         array1[count++] = array[i];
      }
   }
   //sorting 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;
         }
      }
   }
   for ( i = 0; i < count; ++i) {
      printf("%d ", array1[i]);
   }
}
int main() {
   int array[] = {4, 6, 5, 3, 4, 5, 2, 8, 7, 0};
   int n = sizeof(array)/sizeof(array[0]);
   printDistinctElements(array, n);
   return 0;
}
Copy after login

Output

If we run the above program , which will generate the following output.

0 2 3 4 5 6 7 8
Copy after login

The above is the detailed content of Print unique elements in a sorted array in C. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template