当前位置:网站首页>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 .
边栏推荐
- Take you ten days to easily complete the go micro service series (II)
- 网络安全-扫描与密码爆破2
- 【Camera专题】HAL层-addChannel和startChannel简析
- Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
- [data mining] task 2: mimic-iii data processing of medical database
- Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
- In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
- [untitled]
- STM32 - Application of external interrupt induction lamp
- DDL basic operation
猜你喜欢

Analysis, use and extension of open source API gateway apisex

One of the C language practical projects is greedy snake

How is the mask effect achieved in the LPL ban/pick selection stage?

The testing process that software testers should know
![[shutter] animation animation (animatedwidget animation use process | create animation controller | create animation | create animatedwidget animation component | animation operation)](/img/5e/1d451a820eadbd05112b41bd0bc7d6.gif)
[shutter] animation animation (animatedwidget animation use process | create animation controller | create animation | create animatedwidget animation component | animation operation)

Ni visa fails after LabVIEW installs the third-party visa software

Anna: Beibei, can you draw?

Button button adaptive size of wechat applet

Rockchip3399 start auto load driver
![[camera topic] turn a drive to light up the camera](/img/d3/7aabaa5c75813abc4a43820b4c3706.png)
[camera topic] turn a drive to light up the camera
随机推荐
Network security NAT network address translation
可视化yolov5格式数据集(labelme json文件)
7-25 read numbers (loop switch)
2022 financial product revenue ranking
[data mining] task 2: mimic-iii data processing of medical database
Ni visa fails after LabVIEW installs the third-party visa software
Answers to ten questions about automated testing software testers must see
深度学习笔记(持续更新中。。。)
小程序開發的部分功能
[AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)
Query product cases - page rendering data
stm32F407-------ADC
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
Function definition and call, this, strict mode, higher-order function, closure, recursion
Network security - Information Collection
Redis: simple use of redis
Vant implements a simple login registration module and a personal user center
Network security - scanning and password explosion 2
Introduction to kotlin collaboration
ByteDance data Lake integration practice based on Hudi