current position:Home>LeetCode 217. Determine whether there are duplicate elements in the array
LeetCode 217. Determine whether there are duplicate elements in the array
2022-04-29 16:19:09【Wang yuanrou】
Wang yuanrou ==> Personal home page
Come on, everybody , I like to break knowledge into small knowledge points by columns , Just say a piece of knowledge , Therefore, each article is short , I hope you don't mind . If necessary, you can check the column to see if there are articles to explain the knowledge points involved in this article , If you think it helps , I hope you will support me for the third time in a row . |
Recommended reading
Article structure
217. There are duplicate elements
Title Description
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 .
Code implementation
Method 1 double for
const nums = [1, 2, 3, 1];
const nums2 = [1, 2, 3];
// Still shake hands
var containsDuplicate = function(nums) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]) {
return true
}
}
}
return false
};
console.log(containsDuplicate(nums));
console.log(containsDuplicate(nums2));
Method 2Set+size
const nums = [1, 2, 3, 1];
const nums2 = [1, 2, 3];
var containsDuplicate = function(nums) {
// jsSet It has a de duplication effect
return new Set(nums).size == nums.length;
};
console.log(containsDuplicate(nums));
console.log(containsDuplicate(nums2));
Method 3Object+keys()+length
const nums = [1, 2, 3, 1];
const nums2 = [1, 2, 3];
var containsDuplicate = function(nums) {
// js Objects have unique properties
let obj = {
};
for (const iterator of nums) {
obj[iterator] = iterator;
}
return Object.keys(obj).length == nums.length;
};
console.log(containsDuplicate(nums));
console.log(containsDuplicate(nums2));
Method 4forEach()+indexof()+length
const nums = [1, 2, 3, 1];
const nums2 = [1, 2, 3];
var containsDuplicate = function(nums) {
// Traverse to determine whether there are values in the new array , Do not add values repeatedly , Finally, judge whether the length of the new array is equal to that of the array
let arr = [];
nums.forEach(function(item) {
if (arr.indexOf(item) == -1) {
arr.push(item);
}
})
return arr.length == nums.length;
}
console.log(containsDuplicate(nums));
console.log(containsDuplicate(nums2));
Method 5sort()+for
const nums = [1, 2, 3, 1];
const nums2 = [1, 2, 3];
var containsDuplicate = function(nums) {
// Prioritize , Then judge whether the adjacent items are equal
nums.sort();
for (let i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
};
console.log(containsDuplicate(nums));
console.log(containsDuplicate(nums2));

copyright notice
author[Wang yuanrou],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291425343355.html
The sidebar is recommended
- Software design pattern -- Chapter 3 structural pattern -- sharing element pattern
- Vue uses the online form of Excel in the front end of lucky sheet to import, display and export Excel files
- Vue uses echart to draw national maps and overlay charts
- Vue + element UI: Vue user-defined instruction monitors the scrolling event of El table to scroll the scroll bar to the bottom and load new data
- Vue + element when there is no paging at the back end, the front end completes the paging of El table independently - scrolling to the bottom to load new data
- [react] react routing concept
- Lenovo z475 disassembly and repair - plate No. kl6c
- Random array into an array, requiring that the elements cannot be repeated
- The belated Toyota bz4x, even with the blessing of e-tnga architecture, is still not worth starting
- In element plus, for example, how to change the checkbox status in the list by clicking on the header and selecting all
guess what you like
Crawler reverse advanced, using ast technology to restore JavaScript obfuscated code
Help, after changing the user name, the computer is useless and can't log in
Drag the left column of Vue and keep the right width unchanged; The scroll bar appears
HTML notes
In depth analysis of headless single linked list -- dynamic diagram demonstration of C language
Share 9 development skills related to vue3
CSS box centered
Used in Vue projects Sync modifier and $emit (update: XXX)
Vue class & Style binding and computed
Vue project uses this$ forceUpdate(); Force render page
Random recommended
- HTTP becomes HTTPS, self-made certificate
- Web front-end operation - tourism enterprise marketing publicity responsive website template (HTML + CSS + JavaScript)
- Self inspection list of a [qualified] front-end Engineer
- This principle in JavaScript and six common usage scenarios
- JavaScript this priority
- Analyzing the principle of HTTPS encryption
- Difference and principle between websocket and http
- Use of elementui scroll bar component El scrollbar
- Nginx security optimization
- GAC group has become the first pilot enterprise of "yueyouhang". Blessed are the car owners in Guangdong!
- Loki HTTP API usage
- JavaScript - prototype, prototype chain
- Front end experience
- JavaScript -- Inheritance
- HTTP cache
- Filters usage of table in elementui
- A JavaScript pit encountered by a non front-end siege lion
- Grain College - image error when writing Vue with vscode
- Utility gadget - get the IP address in the HTTP request
- Could not fetch URL https://pypi.org/simple/pytest-html/: There was a problem confirming the ssl cer
- Function of host parameter in http
- Use nginx proxy node red under centos7 and realize password access
- Centos7 nginx reverse proxy TCP port
- In eclipse, an error is reported when referencing layuijs and CSS
- Front end online teacher Pink
- Learn to use PHP to insert elements at the specified position and key of the array
- Learn how to use HTML and CSS styles to overlay two pictures on another picture to achieve the effect of scanning QR code by wechat
- Learn how to use CSS to vertically center the content in Div
- Learn how to use CSS to circle numbers
- Learn to open and display PDF files in HTML web pages
- The PHP array random sorting function shuffle() randomly scrambles the order of array elements
- JQuery implements the keyboard enter search function
- 16 ArcGIS API for JavaScript 4.18 a new development method based on ES modules @ ArcGIS / core
- 17 ArcGIS API for JavaScript 4.18 draw points, lines and faces with the mouse
- 18 ArcGIS API for JavaScript 4.18 obtain the length and area after drawing line segments and surface features
- Vue environment construction -- scaffold
- Build a demo with Vue scaffold
- Using vuex in Vue projects
- Use Vue router in Vue project
- 26 [react basics-5] react hook