📌  相关文章
📜  按出现顺序排列所有数组元素串联中出现的所有唯一数字

📅  最后修改于: 2021-04-17 14:20:20             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是打印所有数字的唯一数字,该数字是通过按排在前导零之后的出现顺序将所有数组元素连接起来而形成的。

例子:

方法:想法是将所有数组元素转换为它们的等效字符串,然后将这些字符串连接起来,并使用散列法查找获得的数字中存在的唯一数字。

请按照以下步骤解决问题。

  • 遍历数组ARR []和每个阵列元素转换为它的等效字符串并连接在一个变量中的所有字符串,说S上
  • 使用类型转换将字符串S转换为等效整数(例如N )(删除前导0)
  • 初始化大小为10的哈希表,以存储数字的频率[0,9]
  • lis说,初始化一个空列表
  • 现在,对于数字N的每个数字,增加哈希表中该索引的计数。
  • 对于数字N的每个数字,执行以下操作:
    • 检查是否访问过
    • 如果未访问该数字并且其频率为1,则将此数字附加到lis上使该索引的值为 参观了。
  • 反向列出lis并打印

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to prlong long unique elements
void printUnique(vector 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 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 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 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 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)


输出:
7 6 9 3

时间复杂度:O(N)
辅助空间: O(N)