#K68497. Largest Unique Number

    ID: 32877 Type: Default 1000ms 256MiB

Largest Unique Number

Largest Unique Number

You are given an array of integers. Your task is to find the largest integer that occurs exactly once in the array. If no such integer exists, output -1.

The problem is defined as follows: given a list of integers \(a_1, a_2, \ldots, a_n\), find the maximum integer \(x\) such that the frequency of \(x\) in the list is 1. If every integer appears at least twice, then print \(-1\).

Examples:

  • For input array [4, 9, 2, 9, 7], the largest unique number is 7.
  • For input array [1, 1, 2, 2], there is no unique number, so the output is -1.
  • For input array [1], the result is 1.
  • For input array [-1, -2, -2, -3, -1], the output is -3.
  • For input array [-10, 0, 5, 5, 8, 8, 10, -10, 55, 4, 4], the result is 55.
  • For input array [7, 7, 7, 7], the output is -1.

inputFormat

The input is given via standard input (stdin) in the following format:

n
num1 num2 ... numn

Where n is the number of integers in the array, followed by a line containing n space-separated integers.

outputFormat

Output a single integer via standard output (stdout), which is the largest integer that occurs exactly once in the array. If no such integer exists, output -1.

## sample
5
4 9 2 9 7
7