当前位置:网站首页>There are duplicate elements in leetcode. Go implementation
There are duplicate elements in leetcode. Go implementation
2022-07-02 05:00:00 【Jimmy_ jimi】
The goal is : There are duplicate elements
Give you an array of integers nums . If any value appears in the array At least twice , return true ; If each element in the array is different from each other , return false .
Result output :
### Brute force
func containsDuplicate(nums []int) bool {
flag := false
re:
for i:=0;i<len(nums);i++{
for j:=i+1;j<len(nums);j++{
if nums[i] == nums[j]{
flag =true
break re
}
}
}
return flag
}
Usage sort sort.Ints
func containsDuplicate(nums []int) bool {
sort.Ints(nums)
for i := 1; i < len(nums); i++ {
if nums[i] == nums[i-1] {
return true
}
}
return false
}
Use hash surface
func containsDuplicate(nums []int) bool {
set := map[int]struct{
}{
}
for _, v := range nums {
//set The key value in the table is equal to has Value
if _, has := set[v]; has {
return true
}
set[v] = struct{
}{
}
}
return false
}
边栏推荐
- Learn AI safety monitoring project from zero [attach detailed code]
- Leetcode basic programming: array
- 初学爬虫-笔趣阁爬虫
- Domestic all Chinese automatic test software apifox
- How to modify data file path in DM database
- Common errors of dmrman offline backup
- Mapping location after kotlin confusion
- Record my pytorch installation process and errors
- MMAP zero copy knowledge point notes
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
猜你喜欢

Lay the foundation for children's programming to become a basic discipline

Online incremental migration of DM database

Video multiple effects production, fade in effect and border background are added at the same time

Let genuine SMS pressure measurement open source code

LM09丨费雪逆变换反转网格策略

Analyze the space occupied by the table according to segments, clusters and pages

Practical problem solving ability of steam Education

Virtual machine installation deepin system

Getting started with pytest -- description of fixture parameters

Record the bug of unity 2020.3.31f1 once
随机推荐
CubeMx DMA笔记
What data does the main account of Zhengda Meiou 4 pay attention to?
Lay the foundation for children's programming to become a basic discipline
Unity particle Foundation
Mapping location after kotlin confusion
leetcode存在重复元素go实现
fastText文本分类
C # picture display occupancy problem
Pit encountered in win11 pytorch GPU installation
面试会问的 Promise.all()
Leetcode- insert and sort the linked list
Several methods of capturing packets under CS framework
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Video cover image setting, put cover images into multiple videos in the simplest way
国产全中文-自动化测试软件Apifox
Getting started with pytest -- description of fixture parameters
Feign realizes file uploading and downloading
Analyze the space occupied by the table according to segments, clusters and pages
Idea autoguide package and autodelete package Settings
【ClickHouse】How to create index for Map Type Column or one key of it?