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

📅  最后修改于: 2021-10-27 08:44:41             🧑  作者: Mango

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

例子:

朴素的方法:朴素的想法是遍历给定的数组,并为每个元素从数组中排除当前元素,并使用剩余的数组元素找到所有可能的对(i, j) ,使得arr[i] 等于 arr[ j]i < 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


Javascript


输出:
2 2 3 2 3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程