当前位置:网站首页>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 .
边栏推荐
- 查询商品案例-页面渲染数据
- [data mining] task 4:20newsgroups clustering
- Everything file search tool
- Wordinsert formula /endnote
- [camera topic] complete analysis of camera dtsi
- STM32 - Application of external interrupt induction lamp
- Query product cases - page rendering data
- 【Camera专题】OTP数据如何保存在自定义节点中
- Swift开发学习
- Storage basic operation
猜你喜欢

Analysis, use and extension of open source API gateway apisex

LabVIEW安装第三方VISA软件后NI VISA失效

全链路数字化转型下,零售企业如何打开第二增长曲线
![[Appendix 6 Application of reflection] Application of reflection: dynamic agent](/img/e7/0ee42902b178b13e9a41385267e7b6.jpg)
[Appendix 6 Application of reflection] Application of reflection: dynamic agent

Everything文件搜索工具

Button button adaptive size of wechat applet

Vant 实现简单的登录注册模块以及个人用户中心

微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题

STM32 - GPIO input / output mode

One of the C language practical projects is greedy snake
随机推荐
[AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
转载收录6.5大侠写的部分Qt开发经验
Learn BeanShell before you dare to say you know JMeter
File class (add / delete)
PS去除水印详解
【Camera专题】手把手撸一份驱动 到 点亮Camera
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
Types of map key and object key
Answers to ten questions about automated testing software testers must see
树形结构数据的处理
浏览器是如何对页面进行渲染的呢?
Network security NAT network address translation
ByteDance data Lake integration practice based on Hudi
网络安全-NAT网络地址转换
Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
深度(穿透)选择器 ::v-deep/deep/及 > > >
LabVIEW安装第三方VISA软件后NI VISA失效
技术大佬准备就绪,话题C位由你决定
One of the C language practical projects is greedy snake