Sobes.tech
Middle

# 1. Python. Find duplicate numbers # Description: # Implement a function that searches for repeating numbers in a set # Numbers are initially recorded as a list. # Output the result to the screen. # Input data: # [1, 2, 3, 2, 4, 1, 5, 2] # Result: # [1, 2]

sobes.tech AI

Answer from AI

python def find_duplicates(numbers): seen = set() duplicates = set() for num in numbers: if num in seen: duplicates.add(num) else: seen.add(num) return list(duplicates)

Example usage

nums = [1, 2, 3, 2, 4, 1, 5, 2] print(find_duplicates(nums)) # Will output: [1, 2]

The function uses two sets: one for tracking numbers already seen, and another for duplicates. As it iterates through the list, if a number has been seen before, it is added to the duplicates set.