Qualcomm Interview Question
Software Engineer / DevelopersIf the array is of length n and contains elements between 0 to n - 1 then we can do it by sequentially traversing the array and making elements -ve. If we find an already marked -ve element then that is duplicate
1 3 4 2 3
Marking a[1] -ve ==> 1 -3 4 2 3
Marking a[3] -ve ==> 1 -3 4 -2 3
Marking a[4] -ve ==> 1 -3 4 -2 -3
Marking a[2] -ve ==> 1 -3 -4 -2 -3
See that a[3] is already -ve => 3 is duplicate element
Good one. taking value as array index is the basis of hashing technique.
- Mac March 01, 2010