Categories: Uncategorized

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.

rajendra

Share
Published by
rajendra

Recent Posts

Jump Search Algorithm In Java

Jump search algorithm is a pretty new algorithm to search for an element in a…

1 year ago

Knuth Morris Pratt Pattern Search Algorithm

What is Knuth Morris Pratt or KMP algorithm ? KMP is an algorithm which is…

1 year ago

Binary Search Algorithm In Java

Binary Search is a Logarithmic search which finds the target element in a sorted array…

1 year ago

Leetcode Integer to Roman Java Solution

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X…

2 years ago

Leetcode Container With Most Water Java Solution

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such…

2 years ago

Leetcode Binary Tree Level Order Traversal II Java Solution

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left…

2 years ago

This website uses cookies.