c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字

该博客介绍了如何用C++编写一个程序,在最小的时间复杂度O(n)下,通过哈希映射找出并打印数组中出现一次的非重复数字。示例解释了算法的工作原理,并提供了代码实现。
摘要由CSDN通过智能技术生成

c语言++数组名【数字】

Problem statement: Write a C++ program to print all the non-repeated numbers 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:
    Non-repeated numbers are: 7, 6, 4

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. (0 ->1)

    将其作为键插入并增加频率。 (0-> 1)

    For same key it will only increase frequencies.

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

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

    现在要打印非重复字符,我们需要打印值(频率)正好为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 (the array element)
    END IF
    
    

Time complexity: O(n)

时间复杂度:O(n)

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

Thus, Elements with frequency 1 are: 7, 6, 4

因此,频率为1的元素为:7、6、4

C ++实现可在数组中按频率打印所有非重复数字 (C++ implementation to print all the Non-Repeated Numbers with Frequency in an Array)

#include <bits/stdc++.h>

using namespace std;


void findNonRepeat(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<<"the nonrepeating numbers are: ";
    //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 non-repeating element
    printf("%d ",it->first);
    
    printf("\n");
    
}

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
    findNonRepeat(a,n);

    return 0;
}

Output

输出量

enter array length
10
input array elements...
2 5 3 2 4 5 3 6 7 3
the nonrepeating numbers are: 7 6 4


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

c语言++数组名【数字】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值