# 第一种：使用双重for循环去重

``````public class NewArr1 {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 2, 4, 1, 5};
int len = arr.length;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (arr[i] == arr[j]) {
arr[j] = arr[len - 1];
len--;
j--;
}
}
}
int[] newArr = new int[len];
for (int i = 0; i < len; i++) {
newArr[i] = arr[i];
}
for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i]+" ");
}
}
}``````

# 第二种：使用java8的stream流去重

``````import java.util.Arrays;

public class NewArr2 {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 2, 4, 1, 5};
Integer[] newArr = Arrays.stream(arr).distinct().toArray(Integer[]::new);
for (int i = 0; i < newArr.length; i++) {
if (i == (newArr.length - 1)) {
System.out.print(newArr[i]);
} else {
System.out.print(newArr[i] + ",");
}
}
}
}``````

# 第三种：使用HashSet集合去重

``````import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class NewArr3 {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 2, 4, 1, 5};
Set<Integer> set = new HashSet<>(Arrays.asList(arr));
Integer[] newArr = set.toArray(new Integer[0]);
for (int i = 0; i < newArr.length; i++) {
if (i == (newArr.length - 1)) {
System.out.print(newArr[i]);
} else {
System.out.print(newArr[i] + ",");
}
}
}
}``````

HashSet是一种基于哈希表实现的Set接口。它通过hashCode()方法来确定元素在集合中的存储位置，通过equals()方法来判断元素是否相同。

THE END

)">