无序数组变成有序数组并且去重
面试2:用代码实现无序数组变成有序数组并且去重
例如:{5, 6, 8, 9, 6, 5, 4, 9, 8, 7}
结果:[4, 5, 6, 7, 8, 9]
import java.util.*;
public class Interview{
public static void main(String[] args) {
int[] strs = {5, 6, 8, 9, 6, 5, 4, 9, 8, 7};
//对数组进行排序
Arrays.sort(strs);
HashSet hashSet = new HashSet();
//HashSet不允许储存重复元素
for (int str : strs) {
hashSet.add(str);
}
Object[] objects = hashSet.toArray();
System.out.println(Arrays.toString(objects));
}
}