题解 | #序列中整数去重#

序列中整数去重

http://www.nowcoder.com/practice/6564a2f6e70f4153ad1ffd58b2b28490

题解:使用LinkeHashSet可以保持原来顺序,又可以去重

import java.util.*;
public class Main{
    public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int[] a = new int[n];
           for(int i = 0; i < n; i++){
               a[i] = sc.nextInt();
            }
             sort(a);
        } 
    }
    
    public static void sort(int[] a){
    Set<Integer> set = new LinkedHashSet<>();
         for(int j = 0; j < a.length; j++){
             set.add(a[j]);
            }
        for(Integer i : set){
             System.out.print(i + " ");
        }
    }
}
全部评论

相关推荐

牛客63735620...:只会51能找到工作我吃,了解基本通信协议也远远不够,最最起码得会个stm32吧
点赞 评论 收藏
分享
笑着秋招😊:我一直认为努力有回报是一件很幸福很幸福的事情,恭喜你
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务