Problem Statement
You are given a int[] containing a list of non-negative integers. Find and return the largest one that is duplicated in the list. If there are no duplicates, return -1.
Definition
- Class:
- BiggestDuplicate
- Method:
- findLargest
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int findLargest(int[] x)
- (be sure your method is public)
Constraints
- x will contain between 2 and 50 elements, inclusive.
- Each element of x will be between 0 and 1000, inclusive.
Examples
{ 0, 1, 20, 18, 20, 18, 3 }Returns: 20
The values 18 and 20 are both duplicated, and 20 is the largest of these.{ 15, 12, 3, 15, 7, 12, 15, 7, 12 }Returns: 15
Notice that 15 and 12 each appear many times. Anything appearing more than once is a duplicate.{ 1, 2, 3, 4, 5 }Returns: -1
There are no duplicates here.{ 5, 8, 5, 2 }Returns: 5
Notice that 5, while not the largest element, is the only one that is duplicated.