#325 Finding the Number Without a Pair in an Array

Companies:
Sberbank Diasoft
Themes:
AlgorithmsArraysPractice

An array contains numbers where every number except one has a pair, meaning it occurs twice. Find the number that occurs only once:

// <= [1, 0, 3, -2, 9, 9, 1, -2, 0]
// => 3

int find(int[] arr) {
    ...
}

Additional conditions:

  • null or an empty array -> throw RuntimeException.
  • More than one number without a pair -> throw RuntimeException.
  • No number occurs exactly once -> throw RuntimeException.
  • Any number occurs more than twice -> throw RuntimeException.

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