c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字
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: 10Array input: 2 5 3 2 4 5 3 6 7 3Output:Non-repeated numbers are: 7, 6, 4Solution
解
Data structures used:
使用的數據結構:
Unordered_map <int, int>Key in the map is array value
映射中的鍵是數組值
Value of key is frequency
關鍵值是頻率
Algorithm:
算法:
Declare a map hash to store array elements as keys and to associate their frequencies with them.
聲明地圖哈希,以將數組元素存儲為鍵并將其頻率與它們關聯。
For each array element
對于每個數組元素
Insert it as key & increase frequencies. (0 ->1)
將其作為鍵插入并增加頻率。 (0-> 1)
For same key it will only increase frequencies.
對于相同的鍵,只會增加頻率。
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是值(對應數組值的頻率)
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 maphash[a[i]]++;//for same key increase frequency } .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Actually does the following
實際上是以下
At i=0array[i]=2Insert 2 & increase frequencyHash:Key(element) Value(frequency)2 1At i=1array[i]=5Insert 5 & increase frequencyHash:Key(element) Value(frequency)2 15 1At i=2array[i]=3Insert 3 & increase frequencyHash:Key(element) Value(frequency)2 15 13 1At i=3array[i]=2Insert 2 increase frequency'2' is already there, thus frequency increase.Hash:Key(element) Value(frequency)2 25 13 1At i=4array[i]=4Insert 4 &increase frequencyHash:Key(element) Value(frequency)2 25 13 14 1At i=5array[i]=5'5' is already there, thus frequency increase.Hash:Key(element) Value(frequency)2 25 23 14 1At i=6array[i]=3'3' is already there, thus frequency increase.Hash:Key(element) Value(frequency)2 25 23 24 1At i=7array[i]=6Insert 6, increase frequency.Hash:Key(element) Value(frequency)2 25 23 24 16 1At i=8array[i]=7Insert 7, increase frequency.Hash:Key(element) Value(frequency)2 25 23 24 16 17 1At i=9array[i]=3'3' is already there, thus frequency increase.Hash:Key(element) Value(frequency)2 25 23 34 16 17 1Thus, Elements with frequency 1 are: 7, 6, 4
因此,頻率為1的元素為:7、6、4
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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 mapunordered_map<int,int> hash;for(int i=0;i<n;i++){//creating the maphash[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 elementprintf("%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 frequenciesfindNonRepeat(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語言++數組名【數字】
總結
以上是生活随笔為你收集整理的c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是算术运算和逻辑运算_8086微处理
- 下一篇: java calendar_Java C