📌  相关文章
📜  按排序顺序打印给定数字中存在的所有重复数字

📅  最后修改于: 2021-04-17 13:55:55             🧑  作者: Mango

给定整数N ,任务是按排序顺序打印以N表示的所有重复数字。

例子:

方法:想法是使用哈希存储N的数字的频率并打印那些频率超过1的数字

  • 初始化一个HashMap,以存储数字0-9的频率。
  • 将整数转换为其等效的字符串。
  • 遍历字符串的字符,并且对于每个字符,执行以下步骤:
    • 使用类型转换将当前字符转换为整数
    • 在HashMap中增加该数字的计数。
  • 遍历HashMap并打印计数超过1的数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print repeating
// digits present in the number N
void printDup(string N)
{
  // Stores the count of
  // unique digits
  int res = 0;
 
  // Map to store frequency
  // of each digit
  int cnt[10];
 
  // Set count of all digits to 0
  for (int i = 0; i < 10; i++)
    cnt[i] = 0;
 
  // Traversing the string
  for (int i = 0; i < N.size(); i++) {
 
    // Convert the character
    // to equivalent integer
    int digit = (N[i] - '0');
 
    // Increase the count of digit
    cnt[digit] += 1;
  }
 
  // Traverse the Map
  for (int i = 0; i < 10; i++) {
 
    // If frequency
    // of digit exceeds 1
    if (cnt[i] > 1)
      cout << i << " ";
  }
 
  cout << endl;
}
 
// Driver Code
int main()
{
 
  string N = "45244336543";
 
  // Function call to print
  // repeating digits in N
  printDup(N);
 
  return 0;
}
 
// This code is contributed by Kingash.


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Function to print repeating
  // digits present in the number N
  static void printDup(String N)
  {
    // Stores the count of
    // unique digits
    int res = 0;
 
    // Map to store frequency
    // of each digit
    int cnt[] = new int[10];
 
    // Traversing the string
    for (int i = 0; i < N.length(); i++) {
 
      // Convert the character
      // to equivalent integer
      int digit = (N.charAt(i) - '0');
 
      // Increase the count of digit
      cnt[digit] += 1;
    }
 
    // Traverse the Map
    for (int i = 0; i < 10; i++) {
 
      // If frequency
      // of digit exceeds 1
      if (cnt[i] > 1)
        System.out.print(i + " ");
    }
 
    System.out.println();
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    String N = "45244336543";
 
    // Function call to print
    // repeating digits in N
    printDup(N);
  }
}
 
// This code is contributed by Kingash.


Python3
# Python implementation
# of the above approach
 
# Function to print repeating
# digits present in the number N
def printDup(N):
 
    # Stores the count of
    # unique digits
    res = 0
 
    # Set count of all digits to 0
    cnt = [0] * 10
 
    # Convert integer to
    # equivalent string
    string = str(N)
 
    # Traversing the string
    for i in string:
 
        # Convert the character
        # to equivalent integer
        digit = int(i)
 
        # Increase the count of digit
        cnt[digit] += 1
 
    # Traverse the Map
    for i in range(10):
 
        # If frequency
        # of digit is 1
        if (cnt[i] > 1):
 
            # If count exceeds 1
            print(i, end=" ")
 
 
# Driver Code
 
N = 45244336543
 
# Function call to print
# repeating digits in N
printDup(N)


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
 
  // Function to print repeating
  // digits present in the number N
  static void printDup(string N)
  {
     
    // Stores the count of
    // unique digits
    int res = 0;
 
    // Map to store frequency
    // of each digit
    int[] cnt = new int[10];
 
    // Traversing the string
    for (int i = 0; i < N.Length; i++) {
 
      // Convert the character
      // to equivalent integer
      int digit = (N[i] - '0');
 
      // Increase the count of digit
      cnt[digit] += 1;
    }
 
    // Traverse the Map
    for (int i = 0; i < 10; i++) {
 
      // If frequency
      // of digit exceeds 1
      if (cnt[i] > 1)
        Console.Write(i + " ");
    }
 
    Console.WriteLine();
  }
 
  // Driver code
  public static void Main()
  {
 
    string N = "45244336543";
 
    // Function call to print
    // repeating digits in N
    printDup(N);
  }
}
 
// This code is contributed by code_hunt.


输出:
3 4 5

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