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

0% completed

Vote For New Content
Mohammed Dh Abbas
Better solution

Mohammed Dh Abbas

Jul 20, 2024

class Solution:   def findNumbers(self, nums):     duplicates = []     def swap(i, j):       nums[i], nums[j] = nums[j], nums[i]     for i in range(len(nums)):       # keep swaping while item is not in position and not pointing to an index that has an item that is in position       while nums[i] != i + 1 and nums[i] != nums[nums[i] - 1]:         swap(i, nums[i] - 1)     # if an item is not at the right index add it to the result     for i in range(len(nums)):       if nums[i] != i + 1:         duplicates.append(nums[i])     return duplicates

0

0

Comments
Comments

On this page