1class Solution {2public int thirdMax(int[] nums) {34// taking long data type since array can contain Integer.MIN_VALUE5long max = Long.MIN_VALUE, sMax = Long.MIN_VALUE, tMax = Long.MIN_VALUE;6for (int i : nums) {7if (i > max) {8tMax = sMax;9sMax = max;10max = i;11} else if (i > sMax && i != max) {12tMax = sMax;13sMax = i;14} else if (i > tMax && i != sMax && i != max) {15tMax = i;16}17}1819// if thirdMax was not updated, return the first Max2021return tMax == Long.MIN_VALUE ? (int) max : (int) tMax;22}23}