import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Set<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { set.add(sc.nextInt()); } for (Integer i : set) { System.out.print(i+" "); } } }
import java.util.*; public class Main{ public static void main (String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); HashSet<Integer> set = new HashSet<>(); for (int i = 1; i <= a; i++) { set.add(in.nextInt()); } List<Integer> list = new ArrayList<>(set); Collections.sort(list); for (Integer li : list) { System.out.print(li + " "); } } }
利用Set集合不允许重复元素的特点,来去掉重复元素
import java.util.*; public class Main { public static void main(String[] args){ Scanner s =new Scanner(System.in); int a= s.nextInt(); HashSet set = new HashSet(); for(int i=1;i<=a;i++){ set.add(s.nextInt()); } List list = new ArrayList(set); //升序排序 Collections.sort(list); for (Integer integer : list) { System.out.print(integer+" "); } }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n=scan.nextInt(); int res[]=new int[n]; for(int i=0;i<n;i++){ res[i]=scan.nextInt(); } Arrays.sort(res); System.out.print(res[0]+" "); for(int i=1;i<n;i++){ if(res[i]!=res[i-1]){ System.out.print(res[i]+" "); } } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int nums = 0; List<Integer> list = new ArrayList<>(); while (scan.hasNextInt()){ nums = scan.nextInt(); int num = 0; for (int i = 0; i < nums; i++) { num = Integer.parseInt(scan.next()); if (!list.contains(num)){ list.add(num); } } // 排序 for (int m = 0; m < list.size(); m++){ for (int n = m; n < list.size(); n++){ int tem = list.get(m); if (tem > list.get(n)){ list.set(m, list.get(n)); list.set(n, tem); } } } Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()){ System.out.print(iterator.next() + " "); } } } }
import java.util.*; /** * Created by kingwan on 2020/10/9. * 说明: */ public class Main { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { Set<Integer> set = new HashSet(); ArrayList<Integer> list = new ArrayList(); int n = scanner.nextInt(); while (n>0){ set.add(scanner.nextInt()); n--; } for (Integer integer : set) { list.add(integer); } Collections.sort(list); for (Integer integer : list) { System.out.print(integer+" "); } } }
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
int[] nums = new int[n + 1];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
Arrays.sort(nums);
TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i <= n; i++) { if (set.contains(nums[i])) { continue; } else { set.add(nums[i]); } } for (int m : set) { System.out.print(m + " "); } }
}
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int length=sc.nextInt(); int[] inputArray=generateIntArray(length); Object[] resultArray=simplify(inputArray); Arrays.sort(resultArray); //System.out.println(Arrays.toString(resultArray)); for (int i = 0; i < resultArray.length; i++) { if(i==resultArray.length-1){ System.out.print(resultArray[i]); } else { System.out.print(resultArray[i]+" "); } } } public static int[] generateIntArray(int length){ Scanner sc=new Scanner(System.in); int[] intArray=new int[length+1]; for (int i = 0; i < length; i++) { intArray[i]=sc.nextInt(); } return intArray; } public static Object[] simplify(int[] intArray){ ArrayList<Integer> arrayList=new ArrayList<>(); boolean flag=true; arrayList.add(intArray[0]); for (int i = 1; i < intArray.length; i++) { flag=true; for (int j = 0; j < arrayList.size(); j++) { if (intArray[i]==arrayList.get(j)){ flag=false; break; } } if(flag){ arrayList.add(intArray[i]); } } return arrayList.toArray(); } }IntelliJ IDEA上可以运行,这里报错,不知道为什么
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //输入的数列有多少数据 int x = sc.nextInt(); int [] arr=new int[x]; int res=0; //接受输入的数据 存放在arr数组 for(int i = 0;i<arr.length;i++){ int x1=sc.nextInt(); arr[i]=x1; } //冒泡排序 对数组进行从小到大的顺讯 for (int i =0;i<arr.length;i++){ for (int y = 0;y<arr.length-1-i;y++){ if (arr[y]>arr[y+1]){ res = arr[y+1]; arr[y+1]=arr[y]; arr[y]=res; } } } //利用集合去除数组中重复定位数据 List<Integer> list = new ArrayList<Integer>(); for (int i=0;i<arr.length;i++){ if (!list.contains(arr[i])){ list.add(arr[i]); } } //整数之间用空格分隔输出 for (int i=0;i<list.size();i++){ System.out.print (list.get(i)+" "); } // System.out.println(list.get(list.size()-1)); } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); List<Integer> numList = new ArrayList<>(number); for (int i = 0; i < number; i++) { int param = scanner.nextInt(); if (param <1 || param>1000) { continue; } numList.add(param); } // 去重+排序(从小到大) List<Integer> resultList = numList.stream() .distinct() .sorted() .collect(Collectors.toList()); resultList.forEach(item -> System.out.print(item + " ")); } }
public static void main(String[] args) throws NumberFormatException,IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int[] x=new int[n]; int[] temp =new int[1001]; String[] str=br.readLine().split(" +"); for(int i=0;i<n;i++) { x[i]=Integer.parseInt(str[i]); } for(int i:x) { temp[i]++; } for(int i=0;i<temp.length;i++) { if(temp[i]!=0) System.out.print(i+" "); } } 桶排序真好用
import java.util.*; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextInt()) { int num=sc.nextInt(); int [] arr=new int[num]; for(int i=0;i<num;i++) { arr[i]=sc.nextInt(); } Arrays.sort(arr); LinkedHashSet<Integer> set=new LinkedHashSet<>(); for(int i=0;i<num;i++) { set.add(arr[i]); } for(int item:set) { System.out.print(item+" "); } } } }