I got this question for an exam:
Given an integer array find the first number which is not repeating in array using O(N) time complexity and O(1) space co
I believe the trick to solve the problem is :
max size of array would be 1million
since :
O(1) space means that the memory required by the algorithm is constant
then space complexity will automatically becomes O(1) given the constant 1M. NOTE. 1M is still a constant number even though its a really large number. thus we only need to concentrate on time complexity.
Using a LinkedHashMap we can add a new element with O(1) and retrieve element with O(1) thus updating an entry will take O(1) too. it also preserves the order. therefore, we can find the earliest entry
then the problem will become simple in two steps:
each of the above steps requires O(n) thus overall time complexity is O(2n) = O(n).