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 <= A.length <= 20000 <= A[i] <= 1000Solution:
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.
Jump search algorithm is a pretty new algorithm to search for an element in a…
What is Knuth Morris Pratt or KMP algorithm ? KMP is an algorithm which is…
Binary Search is a Logarithmic search which finds the target element in a sorted array…
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X…
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such…
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left…
This website uses cookies.