Java 在不使用集合的情况下,在不干扰元素顺序的情况下从数组中删除重复项
我需要一个java代码,它可以在不改变元素顺序、不使用集合或排序技术的情况下从数组中删除重复项 例如: 如果输入数组是{23,1,5,4,2,23,6,2,4} 然后输出应该是{23,1,5,4,2,6}创建一个新的ArrayList()。循环使用旧列表并将值复制到新列表,除非该值已在其中 订单将被更改,因为您不会复制所有 创建一个新的ArrayList()。循环使用旧列表并将值复制到新列表,除非该值已在其中Java 在不使用集合的情况下,在不干扰元素顺序的情况下从数组中删除重复项,java,Java,我需要一个java代码,它可以在不改变元素顺序、不使用集合或排序技术的情况下从数组中删除重复项 例如: 如果输入数组是{23,1,5,4,2,23,6,2,4} 然后输出应该是{23,1,5,4,2,6}创建一个新的ArrayList()。循环使用旧列表并将值复制到新列表,除非该值已在其中 订单将被更改,因为您不会复制所有 创建一个新的ArrayList()。循环使用旧列表并将值复制到新列表,除非该值已在其中 订单将被更改,因为您不会复制所有 如果使用Java 8,一个简单的方法是: int[]
订单将被更改,因为您不会复制所有 如果使用Java 8,一个简单的方法是:
int[] array = {23, 1, 5, 4, 2, 23, 6, 2, 4};
int[] noDupes = IntStream.of(array).distinct().toArray();
System.out.println(Arrays.toString(noDupes)); // [23, 1, 5, 4, 2, 6]
如果使用Java 8,一个简单的方法是:
int[] array = {23, 1, 5, 4, 2, 23, 6, 2, 4};
int[] noDupes = IntStream.of(array).distinct().toArray();
System.out.println(Arrays.toString(noDupes)); // [23, 1, 5, 4, 2, 6]
有几种选择-有些比其他更有效:
/**
* Brute force approach - very inefficient.
*
* Finds each duplicate and removes it.
*/
private int[] removeDupesUsingNoExtraMemory(int[] a) {
int length = a.length;
for (int i = 0; i < length - 1; i++) {
for (int j = i + 1; j < length; j++) {
if (a[i] == a[j]) {
// No point copying if this is the last one.
if (j < length - 1) {
// Remove a[j].
System.arraycopy(a, j + 1, a, j, length - j - 1);
}
length -= 1;
}
}
}
// Actually it does use extra memory but only to trim the array because Java cannot slice arrays.
return Arrays.copyOf(a, length);
}
/**
* A bit more efficient.
*
* Copies only non-duplicate ones.
*/
private int[] removeDupesDifferently(int[] a) {
int length = a.length;
// Copying to the end backwards.
int to = length - 1;
// Copy backwards so we remove the second duplicate - not the first.
for (int i = length - 1; i >= 0; i--) {
boolean duplicate = false;
for (int j = 0; j < i && !duplicate; j++) {
duplicate |= a[i] == a[j];
}
if (!duplicate) {
a[to--] = a[i];
}
}
return Arrays.copyOfRange(a, to + 1, a.length);
}
/**
* Most efficient - but uses a `Set`.
*
* Builds a `Set` of `seen` values so we don't copy duplicates.
*/
private int[] removeDupesUsingASet(int[] a) {
int to = 0;
Set<Integer> seen = new HashSet<>();
for (int i = 0; i < a.length - 1; i++) {
// Seen it before?
if (!seen.contains(a[i])) {
a[to++] = a[i];
// Seen that one - don't want to see it again.
seen.add(a[i]);
}
}
return Arrays.copyOf(a, to);
}
public void test() {
System.out.println("removeDupesUsingNoExtraMemory = " + Arrays.toString(removeDupesUsingNoExtraMemory(new int[]{23, 1, 5, 4, 2, 23, 6, 2, 4})));
System.out.println("removeDupesDifferently = " + Arrays.toString(removeDupesDifferently(new int[]{23, 1, 5, 4, 2, 23, 6, 2, 4})));
System.out.println("removeDupesUsingASet = " + Arrays.toString(removeDupesUsingASet(new int[]{23, 1, 5, 4, 2, 23, 6, 2, 4})));
}
/**
*蛮力方法-效率非常低。
*
*查找每个副本并将其删除。
*/
使用非外部内存(int[]a)删除的私有int[]{
int length=a.length;
对于(int i=0;i=0;i--){
布尔重复=假;
对于(int j=0;j
有几种选择-有些比其他更有效:
/**
* Brute force approach - very inefficient.
*
* Finds each duplicate and removes it.
*/
private int[] removeDupesUsingNoExtraMemory(int[] a) {
int length = a.length;
for (int i = 0; i < length - 1; i++) {
for (int j = i + 1; j < length; j++) {
if (a[i] == a[j]) {
// No point copying if this is the last one.
if (j < length - 1) {
// Remove a[j].
System.arraycopy(a, j + 1, a, j, length - j - 1);
}
length -= 1;
}
}
}
// Actually it does use extra memory but only to trim the array because Java cannot slice arrays.
return Arrays.copyOf(a, length);
}
/**
* A bit more efficient.
*
* Copies only non-duplicate ones.
*/
private int[] removeDupesDifferently(int[] a) {
int length = a.length;
// Copying to the end backwards.
int to = length - 1;
// Copy backwards so we remove the second duplicate - not the first.
for (int i = length - 1; i >= 0; i--) {
boolean duplicate = false;
for (int j = 0; j < i && !duplicate; j++) {
duplicate |= a[i] == a[j];
}
if (!duplicate) {
a[to--] = a[i];
}
}
return Arrays.copyOfRange(a, to + 1, a.length);
}
/**
* Most efficient - but uses a `Set`.
*
* Builds a `Set` of `seen` values so we don't copy duplicates.
*/
private int[] removeDupesUsingASet(int[] a) {
int to = 0;
Set<Integer> seen = new HashSet<>();
for (int i = 0; i < a.length - 1; i++) {
// Seen it before?
if (!seen.contains(a[i])) {
a[to++] = a[i];
// Seen that one - don't want to see it again.
seen.add(a[i]);
}
}
return Arrays.copyOf(a, to);
}
public void test() {
System.out.println("removeDupesUsingNoExtraMemory = " + Arrays.toString(removeDupesUsingNoExtraMemory(new int[]{23, 1, 5, 4, 2, 23, 6, 2, 4})));
System.out.println("removeDupesDifferently = " + Arrays.toString(removeDupesDifferently(new int[]{23, 1, 5, 4, 2, 23, 6, 2, 4})));
System.out.println("removeDupesUsingASet = " + Arrays.toString(removeDupesUsingASet(new int[]{23, 1, 5, 4, 2, 23, 6, 2, 4})));
}
/**
*蛮力方法-效率非常低。
*
*查找每个副本并将其删除。
*/
使用非外部内存(int[]a)删除的私有int[]{
int length=a.length;
对于(int i=0;i=0;i--){
布尔重复=假;
对于(int j=0;j
int-arr[]={23,1,5,4,2,6};
列表=新的ArrayList();
对于(inti=0;iintarr[]={23,1,5,4,2,6};
列表=新的ArrayList();
对于(inti=0;i您可以在O(n^2)
时间中使用O(1)额外的空间来完成它
public static int[] noSpaceElementDistinctness(int[] arr) {
int n = arr.length;
for (int i = 0; i < n;i++) {
boolean exists = false;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
exists = true;
break;
}
}
if (exists) {
for (int j = i+1; j<n; j++)
arr[j-1] = arr[j];
n--;
i--; //to iterate the next element, which is now at index i, not i+1.
}
}
for (int i = n; i < arr.length; i++) arr[i] = Integer.MIN_VALUE; //indicates no value
return arr;
}
公共静态int[]nospaceelementdistincity(int[]arr){
int n=阵列长度;
对于(int i=0;i