Function getUniqueUnitDigits

Function getUniqueUnitDigits: The function/method getUniqueUnitDigits accepts two arguments SIZE and arr. SIZE represents the size of the integer array arr.

The function/method getUniqueUnitDigits must return a string containing the unique unit digits in the given array of integers in descending order.

Your task is to implement the function getUniqueUnitDigits so that it passes all the test cases.

IMPORTANT: Do not write the main() function as it is already defined.

Example Input/Output 1:
Input:
8
14 398 840 77 38 132 599 639

Output:
Unique Unit Digits: 987420

Explanation:
The unit digits of the given 8 integers are 4807829 and 9.
The unique unit digits in descending order are 98742 and 0.

Example Input/Output 2:
Input:
5
123 5873 93 543 3331

Output:
Unique Unit Digits: 31

#include <stdio.h>
#include <stdlib.h>
 char* getUniqueUnitDigits(int SIZE, int arr[])
 {
     int unique[10]={0},unit;
     for(int i=0;i<SIZE;i++){
         unit=arr[i]%10;
         if(unique[unit]==0){
             unique[unit]=1;
         }
     }char* unitDigts=malloc(sizeof(char)*10);
     int ptr=0;
     for(int i=9;i>-1;i--){
         if(unique[i]==1){
             unitDigts[ptr++]='0'+i;
         }
     }return unitDigts;
 }
 int main()
 {
     int N;
     scanf("%d", &N);
     int arr[N];
     for(int index = 0; index < N; index++)
     {
         scanf("%d", &arr[index]);
     }
     char *str = getUniqueUnitDigits(N, arr);
     printf("Unique Unit Digits: %s", str);
     free(str);
     return 0;
 }
Previous Article

Matrix - Remove Apples & Oranges

Next Article

Smaller of Two Numbers (Using Ternary Operator)

Write a Comment

Leave a Comment

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