19 Feb 2024

Posted on:

17 Feb 2024

0

# Resolved:Two sum Problem

Does the solution of Two sum Problem provided in course function correctly also if there are repeated numbers in the list, like for example [4,12,6,7,12,43,54] - when I tried to reproduce this list in a dictionary with the code provided here (d[nums[i]] = i), it omits one of the tuples with 12 in this example. I think it overides the second tuple over the first one and then the dictionary looks like this {4: 0, 12: 4, 6: 2, 7: 3, 43: 5, 54: 6}, meaning we miss the first tuple with 12

Instructor
Posted on:

19 Feb 2024

1

Hey Petar,

Thank you for reaching out!

Indeed, the value of the second key-value pair in the dictionary gets overridden. The reason is that keys within a dictionary must be unique. Thus, for this approach to be effective, the list mustn't contain duplicative values.

You're welcome to check out the following discussion on a related (yet, different) issue:

https://365datascience.com/q/a9c00bcf11

Don't hesitate to reach out if you have further questions. I'd be intrigued to see if you come up with an alternative approach or suggestions for improvement.

Kind regards,

365 Hristina