📜  所有数字的频率相同且不同的数字的总和之间的差异

📅  最后修改于: 2021-06-26 21:24:27             🧑  作者: Mango

给定一个由N个整数组成的数组,请找出所有数字的频率相同且不同的数字之和之间的差。例如,对于8844、1001、56、77、34764673是所有数字具有相同频率的数字示例。类似地,545、44199、76672、202是其数字频率不相同的数字的示例。

例子:

方法:遍历数组中的每个元素。保留地图中所有数字的计数。在元素中遍历所有数字后,检查映射是否包含所有数字相同的频率。如果它包含相同的频率,则将其添加到相同的频率,否则将其添加到差异。完全遍历数组后,返回两个元素的差。

下面是上述方法的实现:

CPP
// C++ Difference between the
// summation of numbers
// in which the frequency of
// all digits are same and different
#include 
using namespace std;
  
// Function that returns the difference
int difference(int a[], int n)
{
  
    // Stores the sum of same
    // and different frequency digits
    int same = 0;
    int diff = 0;
  
    // traverse in the array
    for (int i = 0; i < n; i++) {
        // duplicate of array element
        int num = a[i];
        unordered_map mp;
  
        // traverse for every digit
        while (num) {
            mp[num % 10]++;
            num = num / 10;
        }
  
        // iterator pointing to the
        // first element in the array
        auto it = mp.begin();
  
        // count of the smallest digit
        int freqdigit = (*it).second;
        int flag = 0;
  
        // check if all digits have same frequency or not
        for (auto it = mp.begin(); it != mp.end(); it++) {
            if ((*it).second != freqdigit) {
                flag = 1;
                break;
            }
        }
  
        // add to diff if not same
        if (flag)
            diff += a[i];
        else
            same += a[i];
    }
  
    return same - diff;
}
  
// Driver Code
int main()
{
    int a[] = { 24, 787, 2442, 101, 1212 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << difference(a, n);
    return 0;
}


Java
// Java Difference between the
// summation of numbers
// in which the frequency of
// all digits are same and different
import java.util.*;
  
class GFG
{
  
// Function that returns the difference
static int difference(int a[], int n)
{
  
    // Stores the sum of same
    // and different frequency digits
    int same = 0;
    int diff = 0;
  
    // traverse in the array
    for (int i = 0; i < n; i++)
    {
          
        // duplicate of array element
        int num = a[i];
        HashMap mp = new HashMap();
  
        // traverse for every digit
        while (num > 0) 
        {
            if(mp.containsKey(num % 10))
                mp.put(num % 10, (mp.get(num % 10) + 1));
            else
                mp.put(num % 10, 1);
            num = num / 10;
        }
  
        // iterator pointing to the
        // first element in the array
        Iterator> it = mp.entrySet().iterator(); 
          
        // count of the smallest digit
        int freqdigit = it.next().getValue();
        int flag = 0;
  
        // check if all digits have same frequency or not
        for (Map.Entry its : mp.entrySet()) 
        {
            if (its.getValue() != freqdigit) 
            {
                flag = 1;
                break;
            }
        }
  
        // add to diff if not same
        if (flag == 1)
            diff += a[i];
        else
            same += a[i];
    }
  
    return same - diff;
}
  
// Driver Code
public static void main(String[] args)
{
    int a[] = { 24, 787, 2442, 101, 1212 };
    int n = a.length;
    System.out.print(difference(a, n));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 Difference between the
# summation of numbers
# in which the frequency of
# all digits are same and different
  
# Function that returns the difference
def difference(a, n):
      
    # Stores the sum of same
    # and different frequency digits
    same = 0
    diff = 0
      
    # traverse in the array
    for i in range(n):
          
        # duplicate of array element
        num = a[i]
        mp={}
          
        # traverse for every digit
        while (num):
            if num % 10 not in mp:
                mp[num % 10] = 0
            mp[num % 10] += 1
            num = num // 10
              
        # iterator pointing to the
        # first element in the array
        it = list(mp.keys())
          
        # count of the smallest digit
        freqdigit = mp[it[0]]
        flag = 0
          
        # check if all digits have same frequency or not
        for it in mp:
            if mp[it] != freqdigit:
                flag = 1
                break
              
        # add to diff if not same
        if (flag):
            diff += a[i]
        else:
            same += a[i]
      
    return same - diff
  
# Driver Code
a = [24, 787, 2442, 101, 1212] 
n = len(a)
print(difference(a, n))
  
# This code is contributted by SHUBHAMSINGH10


输出:
2790

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。