给定一个包含重复元素的整数数组,任务是找到数组中的重复元素及其频率。
例子:
Input: arr[] = {2, 3, 4, 5, 4, 6, 4, 7, 4, 5, 6, 6}
Output: Below is the frequency of repeated elements –
4 –> 4
5 –> 2
6 –> 3
Input: arr[] = {4, 4, 5, 5, 6}
Output: Below is the frequency of repeated elements –
4 –> 2
5 –> 2
方法:
- 创建一个哈希映射来存储元素的频率。
- 频率大于1的元素为重复元素。
下面是上述方法的实现:
CPP
// CPP Implementation to find the
// repeating elements with there count
#include
using namespace std;
// Function to find the repeating
// elements with there count
map findRepeating(int arr[], int size){
// Hash map to store the
// frequency of elements
map frequency;
// Loop to store the frequency of
// elements of array
for (int i = 0; i < size; i++)
frequency[arr[i]]++;
return frequency;
}
// Driver Code
int main(){
int arr[] = {4, 4, 5, 5, 6};
int arr_size = sizeof(arr)/sizeof(arr[0]);
map frequency = findRepeating(arr, arr_size);
cout<<"Below is the frequency of repeated elements -"< 1)
cout< "<
Java
// Java Implementation to find the
// repeating elements with there count
import java.util.*;
class GFG
{
// Function to find the repeating
// elements with there count
static HashMap findRepeating(int []arr, int size){
// Hash map to store the
// frequency of elements
HashMap frequency = new HashMap();
// Loop to store the frequency of
// elements of array
for(int i = 0; i < size; i++)
{
if(frequency.containsKey(arr[i]))
{
frequency.put(arr[i], frequency.get(arr[i]) + 1);
}
else
{
frequency.put(arr[i], 1);
}
}
return frequency;
}
// Driver Code
public static void main(String []args)
{
int []arr = {4, 4, 5, 5, 6};
int arr_size = arr.length;
HashMap frequency = findRepeating(arr, arr_size);
System.out.println("Below is the frequency"
+"of repeated elements -");
for (Map.Entry entry : frequency.entrySet())
if (entry.getValue() > 1)
System.out.println(entry.getKey()+ " --> "+entry.getValue());
}
}
// This code is contributed by PrinciRaj1992
Python
# Python Implementation to find the
# repeating elements with there count
# Function to find the repeating
# elements with there count
def findRepeating(arr, size):
# Hash map to store the
# frequency of elements
frequency = {}
# Loop to store the frequency of
# elements of array
for i in range (0, size):
frequency[arr[i]] = \
frequency.get(arr[i], 0) + 1
return frequency
# Driver Code
if __name__ == "__main__":
arr = [4, 4, 5, 5, 6]
arr_size = len(arr)
frequency = findRepeating(arr, arr_size)
print("Below is the frequency\
of repeated elements -")
for i in frequency:
if frequency[i] > 1:
print(i, " --> ", frequency[i])
C#
// C# Implementation to find the
// repeating elements with there count
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the repeating
// elements with there count
static Dictionary findRepeating(int []arr, int size){
// Hash map to store the
// frequency of elements
Dictionary frequency = new Dictionary();
// Loop to store the frequency of
// elements of array
for(int i = 0; i < size; i++)
{
if(frequency.ContainsKey(arr[i]))
{
frequency[arr[i]] = frequency[arr[i]] + 1;
}
else
{
frequency.Add(arr[i], 1);
}
}
return frequency;
}
// Driver Code
public static void Main(String []args)
{
int []arr = {4, 4, 5, 5, 6};
int arr_size = arr.Length;
Dictionary frequency = findRepeating(arr, arr_size);
Console.WriteLine("Below is the frequency"
+"of repeated elements -");
foreach (KeyValuePair entry in frequency)
if (entry.Value > 1)
Console.WriteLine(entry.Key+ " --> "+entry.Value);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Below is the frequency of repeated elements -
4 --> 2
5 --> 2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。