当前位置:网站首页>leetcode961. Find the elements repeated N times in the array with length 2n
leetcode961. Find the elements repeated N times in the array with length 2n
2022-07-03 01:57:00 【2021dragon】
Give you an array of integers nums, The array has the following properties :
nums.length == 2*n.numscontain n+1 A different element .numsThere happens to be an element repetition in n Time .
Find and return duplicates n The next element .
Example :
Input :nums = [1, 2, 3, 3]
Output :3
Ideas :
Finding the target element requires two iterations of the array :
- First traversal , Count the number of occurrences of each element .
- Second traversal , Return to appear n Secondary elements , That is, the target element .
The code is as follows :
class Solution {
public:
int repeatedNTimes(vector<int>& nums) {
unordered_map<int, int> countMap;
//1、 First traversal , Count the number of times
for (auto e : nums)
{
countMap[e]++;
}
//2、 Second traversal , Return to appear n Secondary elements
for (auto e : countMap)
{
if (e.second == nums.size() / 2)
return e.first;
}
return -1; // Make sure that the compilation passes ( All paths have return values )
}
};
Explain : According to the question , When executing the code, you must find an occurrence n The second element is returned , This is called execution logic , But when the code is compiled , According to the compilation logic , We must make every logic have a return value , So you need to return an arbitrary value at the end of the code , Its purpose is to make the code compile smoothly , Although we know that the code will not run here to return .
边栏推荐
- Basic operation of view
- Network security - man in the middle attack
- PS去除水印详解
- Custom components, using NPM packages, global data sharing, subcontracting
- How is the mask effect achieved in the LPL ban/pick selection stage?
- Query product cases - page rendering data
- Reprint some Qt development experience written by great Xia 6.5
- 2022 financial product revenue ranking
- Network security - scanning and password explosion 2
- 技术大佬准备就绪,话题C位由你决定
猜你喜欢

Visual yolov5 format data set (labelme JSON file)

What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it

微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
![[Appendix 6 Application of reflection] Application of reflection: dynamic agent](/img/e7/0ee42902b178b13e9a41385267e7b6.jpg)
[Appendix 6 Application of reflection] Application of reflection: dynamic agent

Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly

stm32F407-------ADC

Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)

机器学习笔记(持续更新中。。。)

【数据挖掘】任务1:距离计算

【Camera专题】Camera dtsi 完全解析
随机推荐
2022 financial product revenue ranking
Types of map key and object key
网络安全-动态路由协议RIP
Introduction to kotlin collaboration
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
Where is the future of test engineers? Confused to see
Basic operation of view
树形结构数据的处理
疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
[data mining] task 3: decision tree classification
Technology sharing | Frida's powerful ability to realize hook functions
[camera topic] turn a drive to light up the camera
技术大佬准备就绪,话题C位由你决定
Processing of tree structure data
word插入公式/endnote
File class (check)
STM32 - GPIO input / output mode
[Yu Yue education] Jiujiang University material analysis and testing technology reference
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
浏览器是如何对页面进行渲染的呢?