In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to judge whether there are repeating elements in an integer array by Python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to determine whether there are repeating elements in an integer array by Python".
Question: given an array of integers, how do you determine whether there are duplicate elements?
If any value appears in the array at least twice, the function returns true. Returns false if each element in the array is different.
Example 1:
Input: [1, 2, 3, 1] output: true
Example 2:
Input: [1, 2, 3, 4] output: false
Example 3:
Input: [1, true, 1, 1, 3, 3, 4, 3, 4, 4, 2] output:
Ideas for solving the problem:
Sort the array, and if two consecutive numbers are equal, it proves that there are duplicate elements.
Directly use the hash set: create a new hash collection and add elements to the collection one by one. If the element is not successfully added, it proves that there is a duplicate element and returns True or False.
Code:
The hash set used here to solve the problem
Java:
Class Solution {public boolean containsDuplicate (int [] nums) {Set set = new LinkedHashSet (); for (int num: nums) {if (! set.add (num)) return true; / / failed to join the collection, which proves that there is already a same element in the collection and returns False} return false;}}
Python:
The set () function in Python can directly convert an array into a hash set. Directly compare whether the length of the transformed hash set is equal to the length of the original array, the equality proves that the original array has no repetitive elements, and the unequal proves that the original array contains repetitive elements.
Class Solution: def containsDuplicate (self, nums: List [int])-> bool: return len (nums)! = len (set (nums)) # so far, I believe you have a deeper understanding of "how Python determines whether there are duplicate elements in an integer array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.