C ++程序在数组中打印所有具有频率的重复数字

Problem statement: Write a C++ program to print all the repeated numbers with their frequency in an array in minimum time complexity.

问题陈述:编写一个C ++程序, 以最小的时间复杂度所有重复的数字及其频率打印在数组中

Input Example:

输入示例:

    Array length: 10
    Array input: 2 5 3 2 4 5 3 6 7 3
    Output:
    Repeated number	Frequency
    3			3
    5			2
    2			2

Solution

Data structures used:

使用的数据结构:

    Unordered_map <int, int>

  • Key in the map is array value

    映射中的键是数组值

  • Value of key is frequency

    关键值是频率

Algorithm:

算法:

  1. Declare a map hash to store array elements as keys and to associate their frequencies with them.

    声明地图哈希,以将数组元素存储为键并将其频率与它们关联。

  2.     Unordered_map <int, int>hash;
    
    
  3. For each array element

    对于每个数组元素

    Insert it as key & increase frequencies.

    将其作为键插入并增加频率。

    For same key it will only increase frequencies.

    对于相同的键,只会增加频率。

  4. For i=0: n-1
    	hash[array [i]]++;
    End For
    
    
  5. Now to print the repeated character we need to print the keys (array elements) having value (frequencies) more than 1.

    现在要打印重复的字符,我们需要打印值(频率)大于1的键(数组元素)。

    Set an iterator to

    将迭代器设置为

    hash.begin().

    hash.begin() 。

    iterator->first is the key (array element) & iterator->second is the value( frequency of corresponding array value)

    iterator-> first是键(数组元素)& iterator-> second是值(对应数组值的频率)

  6. IF
        Iterator->second > 1
        Print iterator->first & iterator->second
    END IF
    
    

Explanation with example:

举例说明:

For this array: 2 5 3 2 4 5 3 6 7 3

对于此阵列: 2 5 3 2 4 5 3 6 7 3

The code:

代码:

for(int i=0;i<n;i++){//creating the map
    hash[a[i]]++;//for same key increase frequency
}


Actually does the following

实际上是以下

    At i=0
    array[i]=2
    Insert 2 & increase frequency

    Hash:
    Key(element)	Value(frequency)
    2	            1

    At i=1
    array[i]=5
    Insert 5 & increase frequency

    Hash:
    Key(element)	Value(frequency)
    2	            1
    5	            1


    At i=2
    array[i]=3
    Insert 3 & increase frequency

    Hash:
    Key(element)	Value(frequency)
    2	            1
    5	            1
    3	            1

    At i=3
    array[i]=2
    Insert 2 increase frequency
    '2' is already there, thus frequency increase.

    Hash:
    Key(element)	Value(frequency)
    2	            2
    5	            1
    3	            1

    At i=4
    array[i]=4
    Insert 4 &increase frequency

    Hash:
    Key(element)	Value(frequency)
    2	            2
    5	            1
    3	            1
    4	            1

    At i=5
    array[i]=5
    '5' is already there, thus frequency increase.

    Hash:
    Key(element)	Value(frequency)
    2	            2
    5	            2
    3	            1
    4	            1

    At i=6
    array[i]=3
    '3' is already there, thus frequency increase.

    Hash:
    Key(element)	Value(frequency)
    2	            2
    5	            2
    3	            2
    4	            1

    At i=7
    array[i]=6
    Insert 6, increase frequency.

    Hash:
    Key(element)	Value(frequency)
    2	            2
    5	            2
    3	            2
    4	            1
    6	            1

    At i=8
    array[i]=7
    Insert 7, increase frequency.

    Hash:
    Key(element)	Value(frequency)
    2	            2
    5	            2
    3	            2
    4	            1
    6	            1
    7	            1

    At i=9
    array[i]=3
    '3' is already there, thus frequency increase.

    Hash:
    Key(element)	Value(frequency)
    2	            2
    5	            2
    3	            3
    4	            1
    6	            1
    7	            1


C ++实现以数组的形式打印所有具有频率的重复数字 (C++ implementation to print all the Repeated Numbers with Frequency in an Array)

#include <bits/stdc++.h>

using namespace std;


void findRepeat(int* a, int n){
    //Declare the map
    unordered_map<int,int> hash;
    
    for(int i=0;i<n;i++){//creating the map
        hash[a[i]]++;//for same key increase frequency
    }
    
    cout<<"repeated number      frequency\n";
    //iterator->first == key(element value)
    //iterator->second == value(frequency)
    
    for(auto it=hash.begin();it!=hash.end();it++)
    if(it->second>1)//frequency>1 means repeating element
    printf("%d\t\t\t%d\n",it->first,it->second);
    
}

int main()
{
    int n;
    cout<<"enter array length\n";
    cin>>n;
    int* a=(int*)(malloc(sizeof(int)*n));
    
    cout<<"input array elements...\n";
    
    for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
    
    //function to print repeating elements with their frequencies
    findRepeat(a,n);

    return 0;
}

Output

输出量

enter array length
10
input array elements...
2 5 3 2 4 5 3 6 7 3
repeated number      frequency
3               	3
2               	2
5               	2


翻译自: https://www.includehelp.com/cpp-programs/print-all-the-repeated-numbers-with-frequency-in-an-array.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值