Xiaobai seeks to solve the problem of repetition in LeetCode.

topic description

given an array of integers, 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.

sources of topics and their own ideas

my own idea: loop twice, put the same elements in the new array, and determine whether there is a repetition by judging whether the new array is empty or not.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
arr = []

    for i in range(len(nums)-1):
        for j in range(len(nums)-1):  -sharp i!=j
            if (i!=j) and (nums[i] == nums[j]):
                arr.append(nums[i]) 

if len (arr):

        return True
    else:
        return False
    

what result do you expect? What is the error message actually seen?

The problem with

is that no matter what nums array you enter, the result is false. Xiaobai also asked the great god to answer the reason.

Aug.12,2021

first of all, let me confirm the problem.
is it true that [1 range (len (nums)) 2] returns False?
if so, there is a problem in your code range (len (nums)-1) there is a problem here?
just range (len (nums)) directly and return True. normally

.

if this is the case, there is a problem in your code. Why should I subtract 1?
you can just range (len (nums)) directly and return True. normally.

if this is the case, why do you want to subtract 1?

but you have a big problem with this code. Study it yourself


you have no problem with this way of thinking, range (n) code here n does not include itself, so do not need-1, other words due to indentation is wrong, also not good-looking.

there is no need to bother to check whether it is repeated. Take advantage of the unique characteristics of hashtable, count each element and find that if the count is greater than 1, you can directly return true. The complexity is O (n)

.

do you know that python has a data structure called set (), which can Filter repeating elements. You convert this list to set,. If the length of the set is the same as that of the original list, there are no repeating elements. The difference in length means that there are repeating elements


-sharp 1. pythonset
class Solution(object):
    def hasRepeat(self, nums):
        return len(set(nums)) != len(nums)
.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b41ffa-4dc76.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b41ffa-4dc76.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?