O(1) space not including the input and output variables
The idea is we do a linear pass using the input array itself as a hash to store which numbers have been seen before. We do this by making elements at certain indexes negative. See the full explanation here
class Solution(object): def findDuplicates(self, nums): """ :type nums: List[int] :rtype: List[int] """ res =  for x in nums: if nums[abs(x)-1] < 0: res.append(abs(x)) else: nums[abs(x)-1] *= -1 return res
@Wayne-x Use array itself as hash map to store information. Awesome!
@Wayne-x how do you handle negative elements in an array
@shibu_mahapatra the problem states that all elements are between 1 and n, n being the size of the array.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.