Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Enrique Fernández
Solution with only one loop

Enrique Fernández

Jan 2, 2025

class Solution:   def findNumbers(self, nums):     duplicateNumbers = []     # TODO: Write your code here     i, n = 0, len(nums)     while i < n:       num = nums[i]       if num != nums[num - 1]:         nums[i], nums[num - 1] = nums[num - 1], nums[i]       else:         if num != i + 1:           duplicateNumbers.append(num)         i += 1             return duplicateNumbers

2

0

Comments
Comments

On this page