content: class Solution {
      public int thirdMax(int[] nums) {
          // Sorted set to keep elements in sorted order.
          TreeSet<Integer> sortedNums = new TreeSet<Integer>();
          
          // Iterate on all elements of 'nums' array.
          for (int num : nums) {
              // Do not insert same element again.
              if (sortedNums.contains(num)) {
                  continue;
              }
              
              // If sorted set has 3 elements.
              if (sortedNums.size() == 3) {
                  // And the smallest element is smaller than current element.
                  if (sortedNums.first() < num) {
                      // Then remove the smallest element and push the current element.
                      sortedNums.pollFirst();
                      sortedNums.add(num);
                  }
                  
              } 
              // Otherwise push the current element of nums array.
              else {
                  sortedNums.add(num);
              }
          }
          
          // If sorted set has three elements return the smallest among those 3.
          if (sortedNums.size() == 3) {
              return sortedNums.first();
          }
          
          // Otherwise return the biggest element of nums array.
          return sortedNums.last();
      }
  }