#62 Finding a Duplicate in a List of Numbers

Companies:
Magnit
Themes:
AlgorithmsHash TablesPractice

A List<Integer> containing one duplicate is given. Write a method that finds the duplicate number:

public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = new ArrayList<>();

        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(2);
    }

    public static int findDuplicate(List<Integer> list) {
        ...
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in