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: 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語言++數組名【數字】

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542613.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542613.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542613.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java最接近對點及距離_最接近點對問題_分治法

一、問題描述給定平面上的n個點&#xff0c;找其中的一對點&#xff0c;使得在n個點組成的所有點對中該點對間的距離最小。二、解題思路及所選算法策略的可行性分析思路&#xff1a;利用分治法來解決問題。遞歸子結構求最接近點對總體可分為幾個步驟&#xff1a;1、當問題規模小…

python return用法_初學Python要了解什么 裝飾器知識匯總有哪些

初學Python要了解什么&#xff1f;裝飾器知識匯總有哪些&#xff1f;在Python學習過程中&#xff0c;有多種方法對函數和類進行加工&#xff0c;相對于其它方式&#xff0c;裝飾器語法簡單&#xff0c;代碼可讀性高。因此&#xff0c;裝飾器在Python項目中有廣泛的應用&#xf…

android emulator虛擬設備分析第三篇之pipe上的qemud service

一、概述 本篇和第二篇是強相關的&#xff0c;需要結合第二篇一起看。 以boot-properties為例&#xff0c;注意不需要看ANDROID-QEMUD.TXT&#xff0c;這個是和guest os中的qemud進行相關的&#xff0c;已廢棄。 啟動emulator時&#xff0c;有一個參數-prop <key><val…

c#異常處理_C#異常處理能力問題和解答 套裝4

c#異常處理1) Which is not a valid keyword used in the context of exception handling? trycatchfinalfinally Answer & Explanation Correct answer: 3final The final keyword is not used to handle exceptions in C#.NET. 1)在異常處理的上下文中使用哪個無效關鍵字…

Castor xsd生成java_java – Castor可以處理從基礎XSD導入的多個XSD生成類嗎?

注意&#xff1a;我是EclipseLink JAXB (MOXy)領導者,也是JAXB 2 (JSR-222)專家組的成員.Can Castor do this? If so, what would be the Ant task syntax for it.If not, would perhaps JAXB be a better alternative?下面是如何使用JAXB完成此操作的示例&#xff1a;產品xm…

串口通信 校驗碼_一文讀懂S7-200 SMART自由口通信!

學習S7-200 SMART時了解到&#xff0c;基于RS485接口可實現一下幾種通信&#xff1a;1&#xff09;modbus RTU通信2&#xff09;PPI協議通信3&#xff09;USS協議通信4&#xff09;自由口通信何為自由口通信呢&#xff1f;前三種通信必須要PLC和與其通信的設備支持相同的通信協…

hbase 學習(十三)集群間備份原理

集群建備份&#xff0c;它是master/slaves結構式的備份&#xff0c;由master推送&#xff0c;這樣更容易跟蹤現在備份到哪里了&#xff0c;況且region server是都有自己的WAL 和HLog日志&#xff0c;它就像mysql的主從備份結構一樣&#xff0c;只有一個日志來跟蹤。一個master集…

python expect模塊_Python基礎教程:用Python怎么telnet到網絡設備

Python基礎教程&#xff1a;用Python怎么telnet到網絡設備0.前言Telnet協議屬于TCP/IP協議族里的一種&#xff0c;對于我們這些網絡攻城獅來說&#xff0c;再熟悉不過了&#xff0c;常用于遠程登陸到網絡設備進行操作&#xff0c;但是&#xff0c;它的缺陷太明顯了&#xff0c;…

Java實現動態加載頁面_[Java教程]動態加載頁面數據的小工具 javascript + jQuery (持續更新)...

[Java教程]動態加載頁面數據的小工具 javascript jQuery (持續更新)0 2014-05-07 18:00:06使用該控件&#xff0c;可以根據url&#xff0c;參數&#xff0c;加載html記錄模板(包含json參數對應&#xff0c;以及具體記錄位置Index根據參數描述加載對應的屬性&#xff0c;并可以…

馬哥linux第六周作業

1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄&#xff0c;將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#&#xff1b;[rootmageedu tmp]# cp /etc/rc.d/rc.sysinit . [rootmageedu tmp]# vim rc.sysinit :% s/^[[:space:]]/#&/ #按Esc進入vi…

Java ObjectInputStream enableResolveObject()方法與示例

ObjectInputStream類enableResolveObject()方法 (ObjectInputStream Class enableResolveObject() method) enableResolveObject() method is available in java.io package. enableResolveObject()方法在java.io包中可用。 enableResolveObject() method is used to enable th…

pygame render怎么顯示中文_PyGame開發游戲(2D)02.基礎圖元

這節將介紹PyGame的基礎架構。并學習如何在PyGame里繪制各種幾何圖形和顯示加載圖片。01.應用框架上一節的示例程序里&#xff0c;我們用到一個PyGame的應用程序框架。這是一個基礎框架&#xff0c;利用它我們可以很輕松的添加各類圖型繪制&#xff0c;鍵盤鼠標輸入處理和各類邏…

word+增加水印+java_為Word2019文檔添加水印的兩種方法

水印的類型包括文字水印和圖片水印兩種。在Word文檔中添加文字水印時&#xff0c;可以使用程序中預設的水印效果&#xff0c;而圖片水印則需要自定義添加。一、使用程序預設的文字水印Word 2019中預設了機密、緊急、免責聲明三種類型的文字水印&#xff0c;用戶可根據文件的類型…

如何設置CentOS 7獲取動態及靜態IP地址

自動獲取動態IP地址1.輸入“ip addr”并按回車鍵確定&#xff0c;發現無法獲取IP(CentOS 7默認沒有ifconfig命令)&#xff0c;記錄下網卡名稱&#xff08;本例中為ens33&#xff09;。2.輸入“cd /etc/sysconfig/network-scripts/”按回車鍵確定&#xff0c;繼續輸入“ls”按回…

請求列出指定服務器上的可用功能失敗_濫用 ESI 詳解(上)

在進行安全性評估時&#xff0c;我們注意到了標記語言 Edge Side Includes (ESI)中的一個意外行為&#xff0c;這種語言用于許多流行的 HTTP 代理(反向代理、負載平衡器、緩存服務器、代理服務器)。我們發現成功的 ESI 攻擊可以導致服務器端請求偽造(SSRF)、各種繞過 HTTPOnly …

Java ClassLoader setPackageAssertionStatus()方法與示例

ClassLoader類setPackageAssertionStatus()方法 (ClassLoader Class setPackageAssertionStatus() method) setPackageAssertionStatus() method is available in java.lang package. setPackageAssertionStatus()方法在java.lang包中可用。 setPackageAssertionStatus() metho…

java上傳kafka的方法_哪種方法是將所有數據從Kafka主題復制到接收器(文件或Hive表)的最佳方法?...

我正在使用Kafka Consumer API將所有數據從Kafka主題復制到Hive表 . 為此&#xff0c;我使用HDFS作為中間步驟 . 我使用唯一的組ID并將偏移重置為“最早”&#xff0c;以便從頭開始獲取所有數據&#xff0c;并在執行后忽略提交 . 然后我遍歷Kafka主題中的記錄&#xff0c;并將每…

openstack nova-network 的小bug的排錯經歷

環境是 nova-network vmwareflatdhcp錯誤表現為 開出來的虛擬機有一定幾率獲取不到dhcp地址&#xff0c;手工賦予ip則正常&#xff0c;用flat模式注入的ip正常&#xff0c;下面是排錯過程1首先找網絡防火墻已經把 dnsmasq對應的端口已經打開抓包結果&#xff1a;可以看到虛擬機…

anaconda base環境_anaconda中安裝packages:pip還是conda install?

conda install我就不說了&#xff0c;這都不會別學了就。Using command:$ which -a pip, the terminal will return:This indicates two different pip path to install packages[1].在tf23環境中pip install在base環境中pip install在windows下powershell內&#xff0c;進入到…

Java ClassLoader setDefaultAssertionStatus()方法與示例

ClassLoader類setDefaultAssertionStatus()方法 (ClassLoader Class setDefaultAssertionStatus() method) setDefaultAssertionStatus() method is available in java.lang package. setDefaultAssertionStatus()方法在java.lang包中可用。 setDefaultAssertionStatus() metho…