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.

    Become a Premium Member

    With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

    “I got my App Academy acceptance yesterday, and Coderbyte was definitely a huge help.” ― Tomer
    • 200+ Coding Challenges
    • Mock Interview Questions
    • 500,000+ Code Solutions
    • Algorithm Tutorials
    • Interview Prep Courses
    mrdaniel published this on 11/11/15 | duplicate, array, search, Amazon, Facebook
  • +
  • 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
  • -
  • duplicates([1, 21, -4, 103, 21, 4, 1,1]); => [21, 1, 1]; should we expect [21, 1]?
  • +
  • 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