Skip to content
Related Articles

Related Articles

Print all unique digits present in concatenation of all array elements in the order of their occurrence
  • Last Updated : 10 Mar, 2021

Given an array arr[] consisting of N integers, the task is to print all unique digits of the number formed by concatenating all array elements in the order their occurrence after excluding leading zeroes.
 

Examples:

Input: arr[] = {122, 474, 612, 932}
Output: 7 6 9 3
Explanation:
The number formed by concatenating array elements is “122474612932”.
Unique digits present in the number are 7 6 9 3 (in the order of their occurrence).

Input: arr[]={0, 912, 231, 14}
Output: 9 3 4
Explanation:
The number formed by concatenating array elements is “091223114″.
Final number obtained after removal of leading 0s is “91223114”.
Unique digits present in the number are 9 3 4 (in the order of their occurrence).

Approach: The idea is to convert all array elements to their equivalent strings and concatenate those strings and use Hashing to find unique digits present in the number obtained.

Follow the steps below to solve the problem.



  • Traverse the array arr[] and convert each array element to its equivalent string and concatenate all the strings in a variable, say S.
  • Convert string S to equivalent integer (say N) using typecasting (removing leading 0s)
  • Initialize a Hash Table of size 10, to store the frequency of digits [0, 9].
  • Initialize an empty list, say lis
  • Now, for each digit of number N, increment the count of that index in the Hash Table.
  • For each digit of number N, perform the following operations:
    • Check if it is visited or not
    • If the number is not visited and if its frequency is 1, then append this digit to lis. Make the value of that index as visited.
  • Reverse list lis and print it

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to prlong long unique elements
void printUnique(vector<long long> lis)
{
 
    // Reverse the list
    reverse(lis.begin(),lis.end());
 
    // Traverse thee list
    for(long long i:lis)
        cout << i << " ";
}
 
 
// Function which cheeck for
// all unique digits
void checkUnique(string st)
{
 
    // Stores the final number
    vector<long long> lis;
 
       // Stores the count of
    // unique digits
    long long res = 0;
 
    // Converting string to long longeger
    // to remove leading zeros
    long long N = stoll(st);
 
    // Stores count of digits
    vector<long long> cnt(10, 0), cnt1(10, 0);
 
    // Iterate over the digits of N
    while (N > 0)
    {
 
        // Retrieve the last digit of N
        long long rem = N % 10;
 
        // Increase the count
        // of the last digit
        cnt[rem] += 1;
 
        // Remove the last digit of N
        N = N /10;
      }
 
    // Converting string to long longeger again
    N = stoll(st);
 
    // Iterate over the digits of N
    while (N > 0)
    {
 
        // Retrieve the last digit of N
        long long rem = N % 10;
 
        // If the value of this digit
        // is not visited
        if(cnt1[rem] == 0)
        {
 
            // If its frequeency is 1 (unique)
            if(cnt[rem] == 1)
                lis.push_back(rem);
        }
       
        // Mark the digit visited
        cnt1[rem] = 1;
 
        // Remove the last digit of N
        N = N /10;
      }
 
    // Passing this list to prlong long
    // the reversed list
    printUnique(lis);
}
 
// Function to concatenate array elements
void combineArray(vector<long long> lis)
{
 
    // Stores the concatenated number
    string st = "";
 
    // Traverse the array
    for (long long el : lis)
    {
        // Convert to equivalent string
        string ee = to_string(el);
 
        // Concatenate the string
        st = st + ee;
      }
 
    // Passing string to checkUnique function
    checkUnique(st);
}
 
 
// Driver Code
int main()
{
  vector<long long> arr = {122, 474, 612, 932};
 
  // Function call to prlong long unique
  // digits present in the
  // concatenation of array elements
  combineArray(arr);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29.

Python3




# Python implementation
# of above approach
 
# Function to print unique elements
def printUnique(lis):
   
    # Reverse the list
    lis.reverse()
     
    # Traverse thee list
    for i in lis:
        print(i, end =" ")
 
 
# Function which cheeck for
# all unique digits
def checkUnique(string):
    
    # Stores the final number
    lis = []
     
       # Stores the count of
    # unique digits
    res = 0
     
    # Converting string to integer
    # to remove leading zeros
    N = int(string)
     
    # Stores count of digits
    cnt = [0] * 10
 
    # Iterate over the digits of N
    while (N > 0):
 
        # Retrieve the last digit of N
        rem = N % 10
 
        # Increase the count
        # of the last digit
        cnt[rem] += 1
 
        # Remove the last digit of N
        N = N // 10
         
    # Converting string to integer again
    N = int(string)
     
    # Iterate over the digits of N
    while (N > 0):
 
        # Retrieve the last digit of N
        rem = N % 10
 
        # If the value of this digit
        # is not visited
        if(cnt[rem] != 'visited'):
             
            # If its frequeency is 1 (unique)
            if(cnt[rem] == 1):
               
                   
                lis.append(rem)
                 
        # Mark the digit visited
        cnt[rem] = 'visited'
         
        # Remove the last digit of N
        N = N // 10
     
    # Passing this list to print
    # the reversed list
    printUnique(lis)
 
# Function to concatenate array elements
def combineArray(lis):
 
    # Stores the concatenated number
    string = ""
 
    # Traverse the array
    for el in lis:
 
        # Convert to equivalent string
        el = str(el)
 
        # Concatenate the string
        string = string + el
         
    # Passing string to checkUnique function
    checkUnique(string)
 
 
# Driver Code
 
# Input
arr = [122, 474, 612, 932]
 
# Function call to print unique
# digits present in the
# concatenation of array elements
combineArray(arr)
Output: 
7 6 9 3

 

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :