Largest Unique Number Java Solution

Question :

Given an array of integers A, return the largest integer that only occurs once.

If no integer occurs once, return -1.

Example 1:

Input: [5,7,3,12,4,12,11,3,1]
Output: 11
Explanation: 
The maximum integer in the array is 12 but it is repeated. The number 11 occurs only once, so it's the answer.

Example 2:

Input: [9,9,8,8]
Output: -1
Explanation: 
There is no number that occurs only once.

Note:

  1. 1 <= A.length <= 2000
  2. 0 <= A[i] <= 1000

Solution:

To find the largest unique number , first we will take an array of size 1001 as the note says the maximum array length would be 1000, we will loop through the given array and increase the index in the newly created array, once out pass through the array, we parse the newly created array from i=1000 to 0 and would return the first index of the array whose value is 1.

class Solution {
    public int largestUniqueNumber(int[] A) {
        int [] res = new int [1001];
        for(int i = 0; i < A.length; i++){
            res[A[i]]++;
        }
        for(int i = 1000; i >= 0; i--){
            if(res[i] == 1){
                return i;
            }
        }
        return -1;
    }
}

This is the Largest Unique Number Java Solution in the below post.