📜  给定数字中的重复数字计数

📅  最后修改于: 2021-05-04 16:37:27             🧑  作者: Mango

给定数字N ,任务是计算给定数字中重复数字的总数。

例子:

天真的方法:这个想法是使用两个嵌套循环。在第一个循环中,从数字的第一个数字到最后一个数字一个一个地遍历。然后,对于第一个循环中的每个数字,运行第二个循环,并搜索该数字是否出现在数字的其他任何地方。如果是,则将所需的计数增加1。最后,打印计算出的计数。

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

高效的方法:想法是使用哈希存储数字的频率,然后对频率等于1以上的数字进行计数。请按照以下步骤解决问题:

  • 创建一个大小为10的数组来存储数字0到9的计数。最初将每个索引存储为0。
  • 现在,对于数字N的每个数字,增加数组中该索引的计数。
  • 遍历数组并计算值大于1的索引。
  • 最后,打印此计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that returns the count of
// repeating digits of the given number
int countRepeatingDigits(int N)
{
    // Initialize a variable to store
    // count of Repeating digits
    int res = 0;
 
    // Initialize cnt array to
    // store digit count
 
    int cnt[10] = { 0 };
 
    // Iterate through the digits of N
    while (N > 0) {
 
        // Retrieve the last digit of N
        int rem = N % 10;
 
        // Increase the count of digit
        cnt[rem]++;
 
        // Remove the last digit of N
        N = N / 10;
    }
 
    // Iterate through the cnt array
    for (int i = 0; i < 10; i++) {
 
        // If frequency of digit
        // is greater than 1
        if (cnt[i] > 1) {
 
            // Increment the count
            // of Repeating digits
            res++;
        }
    }
 
    // Return count of repeating digit
    return res;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int N = 12;
 
    // Function Call
    cout << countRepeatingDigits(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
   
// Function that returns the count of
// repeating digits of the given number
static int countRepeatingDigits(int N)
{
    // Initialize a variable to store
    // count of Repeating digits
    int res = 0;
  
    // Initialize cnt array to
    // store digit count
  
    int cnt[] = new int[10];
  
    // Iterate through the digits of N
    while (N > 0)
    {
  
        // Retrieve the last digit of N
        int rem = N % 10;
  
        // Increase the count of digit
        cnt[rem]++;
  
        // Remove the last digit of N
        N = N / 10;
    }
  
    // Iterate through the cnt array
    for (int i = 0; i < 10; i++)
    {
  
        // If frequency of digit
        // is greater than 1
        if (cnt[i] > 1)
        {
  
            // Increment the count
            // of Repeating digits
            res++;
        }
    }
  
    // Return count of repeating digit
    return res;
}
  
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int N = 12;
  
    // Function Call
    System.out.println(countRepeatingDigits(N));
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 program for the above approach
 
# Function that returns the count of
# repeating digits of the given number
def countRepeatingDigits(N):
     
    # Initialize a variable to store
    # count of Repeating digits
    res = 0
 
    # Initialize cnt array to
    # store digit count
    cnt = [0] * 10
 
    # Iterate through the digits of N
    while (N > 0):
 
        # Retrieve the last digit of N
        rem = N % 10
 
        # Increase the count of digit
        cnt[rem] += 1
 
        # Remove the last digit of N
        N = N // 10
     
    # Iterate through the cnt array
    for i in range(10):
 
        # If frequency of digit
        # is greater than 1
        if (cnt[i] > 1):
 
            # Increment the count
            # of Repeating digits
            res += 1
         
    # Return count of repeating digit
    return res
 
# Driver Code
 
# Given array arr[]
N = 12
 
# Function call
print(countRepeatingDigits(N))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
class GFG{
 
// Function that returns the count of
// repeating digits of the given number
static int countRepeatingDigits(int N)
{
    // Initialize a variable to store
    // count of Repeating digits
    int res = 0;
 
    // Initialize cnt array to
    // store digit count
    int []cnt = new int[10];
 
    // Iterate through the digits of N
    while (N > 0)
    {
 
        // Retrieve the last digit of N
        int rem = N % 10;
 
        // Increase the count of digit
        cnt[rem]++;
 
        // Remove the last digit of N
        N = N / 10;
    }
 
    // Iterate through the cnt array
    for (int i = 0; i < 10; i++)
    {
 
        // If frequency of digit
        // is greater than 1
        if (cnt[i] > 1)
        {
 
            // Increment the count
            // of Repeating digits
            res++;
        }
    }
 
    // Return count of repeating digit
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given array []arr
    int N = 12;
 
    // Function Call
    Console.WriteLine(countRepeatingDigits(N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
from collections import Counter
 
# Function that returns the count of
# repeating digits of the given number
def countRepeatingDigits(N):
   
    # converting integer to string
    number = str(N)
     
    # initializing count = 0
    count = 0
    frequency = Counter(number)
     
    # Traversing frequency
    for i in frequency:
        if(frequency[i] > 1):
           
            # increase the count
            count = count+1
    return count
 
# Driver Code
 
 
# Given array arr[]
N = 1232145
 
# Function call
print(countRepeatingDigits(N))
 
# This code is contributed by vikkycirus


输出
0

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

方法2:使用内置的Python函数:

  • 将integer转换为字符串。
  • 使用计数器函数计算字符的频率。
  • 如果频率大于1,则增加计数

下面是实现:

Python3

# Python3 program for the above approach
from collections import Counter
 
# Function that returns the count of
# repeating digits of the given number
def countRepeatingDigits(N):
   
    # converting integer to string
    number = str(N)
     
    # initializing count = 0
    count = 0
    frequency = Counter(number)
     
    # Traversing frequency
    for i in frequency:
        if(frequency[i] > 1):
           
            # increase the count
            count = count+1
    return count
 
# Driver Code
 
 
# Given array arr[]
N = 1232145
 
# Function call
print(countRepeatingDigits(N))
 
# This code is contributed by vikkycirus
输出
2