直接用treeset

import java.util.*;

public class BuyHot{
    public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        TreeSet<Integer> ts = new TreeSet<>();
        //String [] s = new String[n];
        for(int i =0; i<n && sc.hasNext(); i++){
            ts.add(sc.nextInt());
        }
        if (ts.size() < 3){
            System.out.println("-1");
        }
        else{
            int i =0 ;
            for(Integer a : ts){
                if(i == 2)
                    System.out.println(a);
                i++;
            }
        }

    }
    }
}

用hashset去重,用list排序

import java.util.*;
public class Main{
    public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Set<Integer> s = new HashSet<>();
        int k = 0;
        List<Integer> list = new ArrayList<>();
        for(int i =0; i<n && sc.hasNext(); i++){
            k = sc.nextInt();
            if(!s.contains(k)){
                s.add(k);
                list.add(k);
            }
        }
        Collections.sort(list);
        if(list.size()>2){
            System.out.println(list.get(2));
        }else{
            System.out.println("-1");
        }
    }
}