📌  相关文章
📜  为每个数组元素计算数组中所有重复的元素对

📅  最后修改于: 2021-05-14 00:54:51             🧑  作者: Mango

给定一个N个整数的数组arr [] 。对于数组中的每个元素,任务是对可能的对(i,j)进行计数(不包括当前元素),以使i arr [i] = arr [j]

例子:

幼稚的方法:幼稚的想法是遍历给定的数组,对于每个元素,从数组中排除当前元素,并与其余的数组元素一起找到所有可能的对(i,j) ,以使arr [i]等于arr [ j], 。打印每个元素的对数。
时间复杂度: O(N 2 )
辅助空间: O(N)

高效的方法:这个想法是存储每个元素的频率,并在给定条件下计算所有可能的对(例如cnt )。对每个元素执行上述步骤后,从总计数cnt中删除相等的可能对的计数并打印该值。请按照以下步骤解决问题:

  1. 将每个元素的频率存储在Map中。
  2. 创建一个变量来存储每个元素的贡献。
  3. 一些数x的贡献可以被计算为频率[X] *(频率[X] – 1)2,其中频率[X]划分x的频率。
  4. 遍历给定的数组,并从总数中除去每个元素的贡献,并将其存储在ans []中
  5. 打印所有存储在ans []中的元素。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include 
#define int long long int
using namespace std;
 
// Function to print the required
// count of pairs excluding the
// current element
void solve(int arr[], int n)
{
    // Store the frequency
    unordered_map mp;
    for (int i = 0; i < n; i++) {
        mp[arr[i]]++;
    }
 
    // Find all the count
    int cnt = 0;
    for (auto x : mp) {
        cnt += ((x.second)
                * (x.second - 1) / 2);
    }
 
    int ans[n];
 
    // Delete the contribution of
    // each element for equal pairs
    for (int i = 0; i < n; i++) {
 
        ans[i] = cnt - (mp[arr[i]] - 1);
    }
 
    // Print the answer
    for (int i = 0; i < n; i++) {
        cout << ans[i] << " ";
    }
}
 
// Driver Code
int32_t main()
{
    // Given array arr[]
    int arr[] = { 1, 1, 2, 1, 2 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    solve(arr, N);
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to print the required
// count of pairs excluding the
// current element
static void solve(int arr[],
                  int n)
{
  // Store the frequency
  HashMap mp = new HashMap();
  for (int i = 0; i < n; i++)
  {
    if(mp.containsKey(arr[i]))
    {
      mp.put(arr[i], mp.get(arr[i]) + 1);
    }
    else
    {
      mp.put(arr[i], 1);
    }
  }
 
  // Find all the count
  int cnt = 0;
  for (Map.Entry x : mp.entrySet())
  {
    cnt += ((x.getValue()) *
            (x.getValue() - 1) / 2);
  }
 
  int []ans = new int[n];
 
  // Delete the contribution of
  // each element for equal pairs
  for (int i = 0; i < n; i++)
  {
    ans[i] = cnt - (mp.get(arr[i]) - 1);
  }
 
  // Print the answer
  for (int i = 0; i < n; i++)
  {
    System.out.print(ans[i] + " ");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int arr[] = {1, 1, 2, 1, 2};
 
  int N = arr.length;
 
  // Function Call
  solve(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for
# the above approach
 
# Function to prthe required
# count of pairs excluding the
# current element
def solve(arr, n):
     
    # Store the frequency
    mp = {}
    for i in arr:
        mp[i] = mp.get(i, 0) + 1
 
    # Find all the count
    cnt = 0
    for x in mp:
        cnt += ((mp[x]) *
                (mp[x] - 1) // 2)
 
    ans = [0] * n
 
    # Delete the contribution of
    # each element for equal pairs
    for i in range(n):
        ans[i] = cnt - (mp[arr[i]] - 1)
 
    # Print the answer
    for i in ans:
        print(i, end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [1, 1, 2, 1, 2]
 
    N = len(arr)
 
    # Function call
    solve(arr, N)
     
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to print the required
// count of pairs excluding the
// current element
static void solve(int []arr,
                  int n)
{
  // Store the frequency
  Dictionary mp = new Dictionary();
  for (int i = 0; i < n; i++)
  {
    if(mp.ContainsKey(arr[i]))
    {
      mp[arr[i]] =  mp[arr[i]] + 1;
    }
    else
    {
      mp.Add(arr[i], 1);
    }
  }
 
  // Find all the count
  int cnt = 0;
  foreach (KeyValuePair x in mp)
  {
    cnt += ((x.Value) *
            (x.Value - 1) / 2);
  }
 
  int []ans = new int[n];
 
  // Delete the contribution of
  // each element for equal pairs
  for (int i = 0; i < n; i++)
  {
    ans[i] = cnt - (mp[arr[i]] - 1);
  }
 
  // Print the answer
  for (int i = 0; i < n; i++)
  {
    Console.Write(ans[i] + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {1, 1, 2, 1, 2};
 
  int N = arr.Length;
 
  // Function Call
  solve(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
2 2 3 2 3





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