Interview Questions

  • View all interview questions
  • blank
  • Find all duplicates in an array in linear time (v2)
    This is a common interview question where you need to write a program to find all duplicates in an array. The elements in the array have no restrictions, but in this algorithm we'll work specifically with integers. Finding duplicates in an array can be solved in linear time by using a hash table to store each element as we pass through the array. The general algorithm is:


    You need to be a premium member to see the rest of this question and code.

    mrdaniel published this on 11/11/15 | duplicate, array, search, Amazon, Facebook
    Comments
  • +
  • 1
  • -
  • Why do you use
     for i in range(0, len(arr)): 
    instead of
     for i in arr 
    - and reference i instead of arr[i] in the following code?
  • +
  • 0
  • -
  • @jaychiarella, Yes, in this case we can actually use the second line of code you posted because we are not doing anything in particular with the index i. Both lines of code you posted work, but maybe the second one is more readable and easier to understand :)
    Login to submit a comment