当前位置:网站首页>[daily exercise] 217 Duplicate element exists
[daily exercise] 217 Duplicate element exists
2022-06-11 05:46:00 【swindler.】
Given an array of integers , Determine whether there are duplicate elements .
If a value exists, it appears at least twice in the array , The function returns true . If every element in the array is different , Then return to false .
Example 1:
Input : [1,2,3,1]
Output : true
Example 2:
Input : [1,2,3,4]
Output : false
Example 3:
Input : [1,1,1,3,3,4,3,2,4,2]
Output : true
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
var dict = [Int:Int]()
for num in nums {
if dict[num] != nil {
return true
}else {
dict[num] = 1
}
}
return false
}
}
Their thinking : First create a storage nums A dictionary of values and occurrences , And then iterate through the array , Assign the first occurrence of the element key to 1, In the process of traversal , Value is empty (nil) The value of is that the element that has not been assigned value appears only once ; If the value is not empty, it is the element that has been previously assigned , That is, find the elements that appear more than once .
边栏推荐
- ImageView supporting single finger sliding and double finger scaling
- Control your phone with genymotion scratch
- 【深入kotlin】 - 初识 Flow
- What is a thread pool?
- [go deep into kotlin] - flow advanced
- Activity start process record
- Using batch enumeration files
- qmake 实现QT工程pro脚本转vs解决方案
- How to deal with message blackout and message sending failure of Aurora im
- Configure the rust compilation environment
猜你喜欢

20多种云协作功能,3分钟聊透企业的数据安全经

In the future, how long will robots or AI have human creativity?

YOLOv5的Tricks | 【Trick8】图片采样策略——按数据集各类别权重采样

袋鼠云数栈基于CBO在Spark SQL优化上的探索

“All in ONE”一个平台解决所有需求,运维监控3.0时代已来

Write a list with kotlin

深度学习分布式训练

那个酷爱写代码的少年后来怎么样了——走近华为云“瑶光少年”

Getting started with kotlin
![[元数据]LinkedIn-DataHub](/img/6a/247ee77dfc219d26651e5d93b04c98.png)
[元数据]LinkedIn-DataHub
随机推荐
NDK learning notes (VII) system configuration, users and groups
[metadata]linkedin datahub
MySQL circulates multiple values foreach, XML writing method
Install Oracle Database
Managing VHDS using batch
Further efficient identification of memory leakage based on memory optimization tool leakcanary and bytecode instrumentation technology
数组部分方法
SwiftUI: Navigation all know
如何让灯具智能化,单火、零火智能开关怎么选!
Get the value of program exit
Utiliser le fichier Batch Enum
Use of vlayout
27. Remove elements
Manually splicing dynamic JSON strings
Multithreading tutorial (XXVII) CPU cache and pseudo sharing
Array partial method
ImageView supporting single finger sliding and double finger scaling
Flask develops and implements the like comment module of the online question and answer system
Es IK installation error
使用Batch枚舉文件