-3

I´m trying some LeetCode problems and I came across a "Check Duplicates" problem. When I do the tests tho, it doesn't work when the function receives the array [3,3]. I really can't understand why. Here is my code:

def containsDuplicate(nums) -> bool:
        list = {}
        for i in range (len(nums)):
            if(3 in list):
                return True
            else:
                list[i] = nums[i]
                print(list[i])
      
        return False

def main():
    print(containsDuplicate([3,3]))
    


main()

Does anyone know why the "if" statement that's supposed to return True isn't working? PS I know I could use set or something like a hashmap but I just want to understand what I´m doing wrong in this case

  • 3
    The so-called `list` is actually a dictionary here. So `3 in list` checks whether 3 is in the dictionary's keys. Do you intend that, or do you want to check in the values? (btw, best to avoid using `list` as a variable name at all, since it's the name of a builtin type). – slothrop Jul 04 '23 at 20:48
  • 1
    You made `list` a dictionary. Did you mean to? The `in` test on a dict tests if a key is in the dict, not if a value is in the dict. Also, unless you're just testing things, the code isn't going to work correctly as it's always testing for 3 and won't detect duplicates in, for example, [4,4]. – jarmod Jul 04 '23 at 20:48
  • 1
    (Also, it would be indeed be cleaner to use a set rather than either a list or a dict [i.e. hashmap] here.) – slothrop Jul 04 '23 at 20:50
  • 1
    Did you get the key and value mixed up? `list[nums[i]] = i` seems to make sense and works for this case, though there are still problems with the rest of the code. – wjandrea Jul 04 '23 at 20:54
  • Does this answer your question? [Removing duplicates in lists](https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists) – Sebastian Wozny Jul 04 '23 at 20:57
  • If the purpose of your function is only to test if the list contains duplicates, you should probably use a set rather than a dictionary. e.g. `return any(n in seen or seen.add(n) for seen in [set()] for n in nums)` – Alain T. Jul 04 '23 at 20:59

3 Answers3

0

The {} initiates a dictionary, wherein 'I' within the for loop functions as an index. Therefore, by executing list[i] = nums[i], you are generating a dictionary with the key:value pair -> dict{i: nums[i]}. To rectify this, replace list {} with list [], and use the 'append' method to add elements.

def containsDuplicate(nums) -> bool:
    list = [] # Defini a list as a list
    for i in range (len(nums)):
        if(3 in list):
            return True
        else:
            list.append(nums[i]) # Insert the element into the list

    return False
0

list is a dictionary, so the output will be

{0:3, 1:3}

When you check if (3 in list), you check if there is a key with 3, but there is not one.

You can initialize your list with square brackets: my_list=[]. Later on, you will append each element that there is not inside the list like this: my_list.append(nums[i]).

NOTE: Change list to other variable name, because it is a python builtin.
LoukasPap
  • 1,244
  • 1
  • 8
  • 17
  • Noted, it also worked! thanks for answering, also you're right... for future codes I won't use that variable's name – Sebastian Presno Alvarado Jul 05 '23 at 05:06
  • If this answers your question, is it possible to mark it as correct (by clicking the grey tick-mark next to the voting section in my answer)? Thank you @SebastianPresnoAlvarado – LoukasPap Jul 05 '23 at 07:24
0

You're checking for 3 in the dictionary's keys, but your code is writing 3 to its values.

def containsDuplicate(nums) -> bool:
    list = {}
    for i in range (len(nums)):
        if (3 in list.values()):
            return True
        else:
            list[i] = nums[i]
            print(list[i])
    
    return False

def main():
    print(containsDuplicate([3,3]))
    
main()

Updated your code to check the dictionary's values, not keys.

Output:

3
True
Zero
  • 1,807
  • 1
  • 6
  • 17
  • Thank you!! you're right, I needed to check the values. That did the work. So for future reference, while checking for dictionaries values, not keys, i should use the `.values()` function, right? – Sebastian Presno Alvarado Jul 05 '23 at 05:06
  • Yes @SebastianPresnoAlvarado – Zero Jul 05 '23 at 07:41
  • Indeed - it's worth noting though that a dictionary is a hashmap, so looking for an item in the keys is fast (i.e. O(1)) but looking for an item in the values is slow (i.e. O(N)). – slothrop Jul 05 '23 at 08:37