Friday, December 27, 2024
Google search engine
HomeData Modelling & AIPrint all unique digits present in concatenation of all array elements in...

Print all unique digits present in concatenation of all array elements in the order of their occurrence

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 of 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 print long unique elements
void printUnique(vector<long long> lis)
{
 
    // Reverse the list
    reverse(lis.begin(),lis.end());
 
    // Traverse the list
    for(long long i:lis)
        cout << i << " ";
}
 
 
// Function which check 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 longer
    // 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 longer 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 frequency 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 print 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.


Java




// Java program for the above approach
 
import java.util.Arrays;
 
class GFG {
  public static void main(String[] args) {
    long[] arr = { 122, 474, 612, 932 };
    combineArray(arr);
  }
 
  // Function to print long unique elements
  static void printUnique(long[] lis) {
    // Reverse the list
    for (int i = 0; i < lis.length / 2; i++) {
      long temp = lis[i];
      lis[i] = lis[lis.length - i - 1];
      lis[lis.length - i - 1] = temp;
    }
 
    // Traverse the list
    for (long i : lis) {
      if (i != 0) {
        System.out.print(i + " ");
      }
    }
  }
 
  // Function which check for
  // all unique digits
  static void checkUnique(String str) {
    // Stores the final number
    long[] lis = new long[str.length()];
 
    // Stores the count of
    // unique digits
    long res = 0;
 
    // Converting string to long to remove leading zeros
    long N = Long.parseLong(str);
 
    // Stores count of digits
    long[] cnt = new long[10];
 
    // Iterate over the digits of N
    while (N > 0)
    {
 
      // Retrieve the last digit of N
      long rem =  N % 10;
 
      // Increase the count
      // of the last digit
      cnt[(int)rem]++;
 
      // Remove the last digit of N
      N /= 10;
    }
 
    // Converting string to long longer again
    N = Long.parseLong(str);
 
    // Iterate over the digits of N
    while (N > 0)
    {
      // Retrieve the last digit of N
      long rem =  N % 10;
      if (cnt[(int)rem] != 'v')
      {
        if (cnt[(int)rem] == 1)
        {
          lis[(int)res++] = rem;
        }
      }
      cnt[(int)rem] = 'v';
      N /= 10;
    }
 
    // Function to print long unique elements
    printUnique(lis);
  }
 
  // Function to concatenate array elements
  static void combineArray(long[] lis)
  {
    String str = "";
    for (long el : lis)
    {
      str += String.valueOf(el);
    }
    checkUnique(str);
  }
}
 
 
 
 
// This code is contributed by phasing17


Python3




# Python implementation
# of above approach
 
# Function to print unique elements
def printUnique(lis):
   
    # Reverse the list
    lis.reverse()
     
    # Traverse the list
    for i in lis:
        print(i, end =" ")
 
 
# Function which check 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 frequency 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)


C#




using System;
using System.Linq;
 
class Program
{
  static void Main(string[] args)
  {
    long[] arr = { 122, 474, 612, 932 };
    CombineArray(arr);
  }
 
  // Function to print long unique elements
  static void PrintUnique(long[] lis)
  {
    // Reverse the list
    Array.Reverse(lis);
 
    // Traverse the list
    foreach (long i in lis)
    {
      if (i != 0)
        Console.Write(i + " ");
    }
  }
 
  // Function which check for
  // all unique digits
  static void CheckUnique(string str)
  {
    // Stores the final number
    long[] lis = new long[str.Length];
 
    // Stores the count of
    // unique digits
    long res = 0;
 
    // Converting string to long longer
    // to remove leading zeros
    long N = long.Parse(str);
 
    // Stores count of digits
    long[] cnt = new long[10];
 
    // Iterate over the digits of N
    while (N > 0)
    {
 
      // Retrieve the last digit of N
      long rem = N % 10;
 
      // Increase the count
      // of the last digit
      cnt[rem]++;
 
      // Remove the last digit of N
      N /= 10;
    }
 
    // Converting string to long longer again
    N = long.Parse(str);
 
    // Iterate over the digits of N
    while (N > 0)
    {
      // Retrieve the last digit of N
      long rem = N % 10;
      if (cnt[rem] != 'v')
      {
        if (cnt[rem] == 1)
        {
          lis[res++] = rem;
        }
      }
      cnt[rem] = 'v';
      N /= 10;
    }
 
    // Function to print long unique elements
    PrintUnique(lis);
  }
 
  // Function to concatenate array elements
  static void CombineArray(long[] lis)
  {
    string str = "";
    foreach (long el in lis)
    {
      str += el.ToString();
    }
    CheckUnique(str);
  }
}
 
// This code is contributed by phasing17.


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to print long unique elements
function printUnique(lis)
{
 
    // Reverse the list
    lis.reverse();
 
    // Traverse the list
    for(var i=0; i<lis.length; i++)
    {
        document.write(lis[i]+" ")
    }
 
}
 
 
// Function which check for
// all unique digits
function checkUnique(st)
{
 
    // Stores the final number
    var lis = [];
 
       // Stores the count of
    // unique digits
    var res = 0;
 
    // Converting string to long longer
    // to remove leading zeros
    var N = parseInt(st);
 
    // Stores count of digits
    var cnt = Array(10).fill(0);
    var cnt1 = Array(10).fill(0);
 
    // Iterate over the digits of N
    while (N > 0)
    {
 
        // Retrieve the last digit of N
        var rem = N % 10;
 
        // Increase the count
        // of the last digit
        cnt[rem] += 1;
 
        // Remove the last digit of N
        N = parseInt(N /10);
      }
 
    // Converting string to long longer again
    N = parseInt(st);
 
    // Iterate over the digits of N
    while (N > 0)
    {
 
        // Retrieve the last digit of N
        var rem = N % 10;
 
        // If the value of this digit
        // is not visited
        if(cnt1[rem] == 0)
        {
 
            // If its frequency is 1 (unique)
            if(cnt[rem] == 1)
                lis.push(rem);
        }
       
        // Mark the digit visited
        cnt1[rem] = 1;
 
        // Remove the last digit of N
        N = parseInt(N /10);
      }
 
    // Passing this list to print long
    // the reversed list
    printUnique(lis);
}
 
// Function to concatenate array elements
function combineArray(lis)
{
 
    // Stores the concatenated number
    var st = "";
 
    // Traverse the array
    for(var i =0; i< lis.length; i++)
    {
        // Convert to equivalent string
        var ee = (lis[i].toString());
 
        // Concatenate the string
        st = st + ee;
      }
 
    // Passing string to checkUnique function
    checkUnique(st);
}
 
 
// Driver Code
var arr = [122, 474, 612, 932];
// Function call to print long unique
// digits present in the
// concatenation of array elements
combineArray(arr);
 
</script>


Output: 

7 6 9 3

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments