Print all unique digits present in concatenation of all array elements in the order of their occurrence
Last Updated :
10 Jan, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void printUnique(vector< long long > lis)
{
reverse(lis.begin(),lis.end());
for ( long long i:lis)
cout << i << " " ;
}
void checkUnique(string st)
{
vector< long long > lis;
long long res = 0;
long long N = stoll(st);
vector< long long > cnt(10, 0), cnt1(10, 0);
while (N > 0)
{
long long rem = N % 10;
cnt[rem] += 1;
N = N /10;
}
N = stoll(st);
while (N > 0)
{
long long rem = N % 10;
if (cnt1[rem] == 0)
{
if (cnt[rem] == 1)
lis.push_back(rem);
}
cnt1[rem] = 1;
N = N /10;
}
printUnique(lis);
}
void combineArray(vector< long long > lis)
{
string st = "" ;
for ( long long el : lis)
{
string ee = to_string(el);
st = st + ee;
}
checkUnique(st);
}
int main()
{
vector< long long > arr = {122, 474, 612, 932};
combineArray(arr);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
long [] arr = { 122 , 474 , 612 , 932 };
combineArray(arr);
}
static void printUnique( long [] lis) {
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;
}
for ( long i : lis) {
if (i != 0 ) {
System.out.print(i + " " );
}
}
}
static void checkUnique(String str) {
long [] lis = new long [str.length()];
long res = 0 ;
long N = Long.parseLong(str);
long [] cnt = new long [ 10 ];
while (N > 0 )
{
long rem = N % 10 ;
cnt[( int )rem]++;
N /= 10 ;
}
N = Long.parseLong(str);
while (N > 0 )
{
long rem = N % 10 ;
if (cnt[( int )rem] != 'v' )
{
if (cnt[( int )rem] == 1 )
{
lis[( int )res++] = rem;
}
}
cnt[( int )rem] = 'v' ;
N /= 10 ;
}
printUnique(lis);
}
static void combineArray( long [] lis)
{
String str = "" ;
for ( long el : lis)
{
str += String.valueOf(el);
}
checkUnique(str);
}
}
|
Python3
def printUnique(lis):
lis.reverse()
for i in lis:
print (i, end = " " )
def checkUnique(string):
lis = []
res = 0
N = int (string)
cnt = [ 0 ] * 10
while (N > 0 ):
rem = N % 10
cnt[rem] + = 1
N = N / / 10
N = int (string)
while (N > 0 ):
rem = N % 10
if (cnt[rem] ! = 'visited' ):
if (cnt[rem] = = 1 ):
lis.append(rem)
cnt[rem] = 'visited'
N = N / / 10
printUnique(lis)
def combineArray(lis):
string = ""
for el in lis:
el = str (el)
string = string + el
checkUnique(string)
arr = [ 122 , 474 , 612 , 932 ]
combineArray(arr)
|
C#
using System;
using System.Linq;
class Program
{
static void Main( string [] args)
{
long [] arr = { 122, 474, 612, 932 };
CombineArray(arr);
}
static void PrintUnique( long [] lis)
{
Array.Reverse(lis);
foreach ( long i in lis)
{
if (i != 0)
Console.Write(i + " " );
}
}
static void CheckUnique( string str)
{
long [] lis = new long [str.Length];
long res = 0;
long N = long .Parse(str);
long [] cnt = new long [10];
while (N > 0)
{
long rem = N % 10;
cnt[rem]++;
N /= 10;
}
N = long .Parse(str);
while (N > 0)
{
long rem = N % 10;
if (cnt[rem] != 'v' )
{
if (cnt[rem] == 1)
{
lis[res++] = rem;
}
}
cnt[rem] = 'v' ;
N /= 10;
}
PrintUnique(lis);
}
static void CombineArray( long [] lis)
{
string str = "" ;
foreach ( long el in lis)
{
str += el.ToString();
}
CheckUnique(str);
}
}
|
Javascript
<script>
function printUnique(lis)
{
lis.reverse();
for ( var i=0; i<lis.length; i++)
{
document.write(lis[i]+ " " )
}
}
function checkUnique(st)
{
var lis = [];
var res = 0;
var N = parseInt(st);
var cnt = Array(10).fill(0);
var cnt1 = Array(10).fill(0);
while (N > 0)
{
var rem = N % 10;
cnt[rem] += 1;
N = parseInt(N /10);
}
N = parseInt(st);
while (N > 0)
{
var rem = N % 10;
if (cnt1[rem] == 0)
{
if (cnt[rem] == 1)
lis.push(rem);
}
cnt1[rem] = 1;
N = parseInt(N /10);
}
printUnique(lis);
}
function combineArray(lis)
{
var st = "" ;
for ( var i =0; i< lis.length; i++)
{
var ee = (lis[i].toString());
st = st + ee;
}
checkUnique(st);
}
var arr = [122, 474, 612, 932];
combineArray(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...