Second largest distinct
Java
Medium
6 views
Problem Description
Task: return the second largest distinct value. If it does not exist, return Integer.MIN_VALUE.
Output Format
Return value
Constraints
Works with duplicates and negatives.
Official Solution
static int secondLargestDistinct(int[] a){int first=Integer.MIN_VALUE,second=Integer.MIN_VALUE;for(int x:a){if(x>first){second=first;first=x;}else if(x!=first && x>second){second=x;}}return second;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!