# Awesome java solution in 2 ways to deal with the corner case

• the corner case {1,2} and {1,2,Integer.MIN_VALUE}, can be dealt in 2 ways:

``````    public int thirdMax(int[] nums) {
long[] a = new long[3];
Arrays.fill(a, Long.MIN_VALUE);
for (int num : nums) {
if (num != a[0] && num != a[1] && num != a[2]) {
if (num > a[0]) {
a[2] = a[1];
a[1] = a[0];
a[0] = num;
} else if (num > a[1]) {
a[2] = a[1];
a[1] = num;
} else if (num > a[2]) {
a[2] = num;
}
}
}
if (a[2] == Long.MIN_VALUE) {
return (int) a[0];
}
return (int) a[2];
}
``````

2.add an additive variable to track the global minimum value of the array

``````    public int thirdMax(int[] nums) {
int[] a = new int[3];
Arrays.fill(a, Integer.MIN_VALUE);
int min = Integer.MAX_VALUE;
for (int num : nums) {
if (num != a[0] && num != a[1] && num != a[2]) {
if (num > a[0]) {
a[2] = a[1];
a[1] = a[0];
a[0] = num;
} else if (num > a[1]) {
a[2] = a[1];
a[1] = num;
} else if (num > a[2]) {
a[2] = num;
}
}
min = Math.min(min, num);
}
/**
* a[2]>min indicates two cases:
* 1.There are equal to or more than 3 distinct elements in the nums array
* 2.There are less than 3 distinct elements but the minimum value is Integer.MIN_VALUE
* by adding the constraint a[1]>a[2](or a[1]!=a[2]), we are sure:
* case 2 is impossible
* only case 1 is possible
*/
if (a[2] >= min && a[1] != a[2]) {
return a[2];
}
return a[0];
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.