从数组中删除左侧重复项
Removing left sided duplicates from array
我正在尝试查找我的代码中的错误,我想知道您是否可以帮助我。我的任务是编写一个方法,它将一个数组作为输入,并且 return 这个数组没有左重复项(最后一个数字保留)。如果输入是 ( 1,2,1,2,1,2,3 ) It returns (1, 1, 2, 3) 而不是 1,2,3,我的代码将不起作用。这是代码
public static int [] solve(int [] arr){
boolean check=false;
ArrayList<Integer> test = new ArrayList<>(); // idk how to compete this task without ArrayList
for (int i = 0; i < arr.length; i++) { // Here i pass my array to ArrayList
test.add(arr[i]);
}
while(check==false) {
for (int i = 0; i < test.size(); i++) {
for (int j = i + 1; j < test.size(); j++) { // Somewhere here must be my mistake that i can't find
check=true;
if (test.get(i) == test.get(j)) {
test.remove(i);
check=false;
}
}
}
}
// i created second array to return array without duplcates.
int arr2[];
arr2=new int[test.size()];
for(int i=0;i<arr2.length;i++){
arr2[i]=test.get(i);
}
return arr2;
}
}
我试图自己完成这个任务,所以直到现在我才使用 Google 来寻求帮助。如果您知道如何改进我的代码,请随意更改您想要的一切。提前致谢!
您可以创建第二个列表,循环访问您的输入列表,并为输入列表的每个元素检查第二个列表是否包含它。如果没有,请将其添加到第二个列表。这通过了您在原始 post:
中提到的测试用例
import java.util.*;
public class MyClass {
public static void main(String args[]) {
List<Integer> test = new ArrayList<>();
List<Integer> noduplicates = new ArrayList<>();
test.add(1);
test.add(2);
test.add(1);
test.add(2);
test.add(1);
test.add(2);
test.add(3);
for (Integer i : test) {
if (!noduplicates.contains(i)) {
noduplicates.add(i);
}
}
System.out.println(noduplicates);
}
}
HashSet can do this since you cant put duplicate elements to HashSet, all you need to do is put your array into HashSet.
List<Integer> arr2 = new ArrayList<>(new HashSet<>(arr));
一个简单的方法是
将数组的数字以相反的顺序添加到 LinkedHashSet
以仅保留唯一数字并保留每个唯一数字的最后一个条目。
从 LinkedHashSet
中创建一个 List
。
使用Collections#reverse
反转List
。
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new LinkedHashSet<>();
int[] arr = { 7, 7, 4, 5, 7, 2, 7 };
// Add the numbers of the array in reverse order to a LinkedHashSet in order to remove
// duplicates as well as to preserve the last entry of each unique number.
for (int i = arr.length - 1; i >= 0; i--) {
set.add(arr[i]);
}
// Create a List out of the LinkedHashSet
List<Integer> list = new ArrayList<>(set);
// Reverse the List
Collections.reverse(list);
// Display the result
System.out.println(list);
}
}
输出:
[4, 5, 2, 7]
我正在尝试查找我的代码中的错误,我想知道您是否可以帮助我。我的任务是编写一个方法,它将一个数组作为输入,并且 return 这个数组没有左重复项(最后一个数字保留)。如果输入是 ( 1,2,1,2,1,2,3 ) It returns (1, 1, 2, 3) 而不是 1,2,3,我的代码将不起作用。这是代码
public static int [] solve(int [] arr){
boolean check=false;
ArrayList<Integer> test = new ArrayList<>(); // idk how to compete this task without ArrayList
for (int i = 0; i < arr.length; i++) { // Here i pass my array to ArrayList
test.add(arr[i]);
}
while(check==false) {
for (int i = 0; i < test.size(); i++) {
for (int j = i + 1; j < test.size(); j++) { // Somewhere here must be my mistake that i can't find
check=true;
if (test.get(i) == test.get(j)) {
test.remove(i);
check=false;
}
}
}
}
// i created second array to return array without duplcates.
int arr2[];
arr2=new int[test.size()];
for(int i=0;i<arr2.length;i++){
arr2[i]=test.get(i);
}
return arr2;
}
}
我试图自己完成这个任务,所以直到现在我才使用 Google 来寻求帮助。如果您知道如何改进我的代码,请随意更改您想要的一切。提前致谢!
您可以创建第二个列表,循环访问您的输入列表,并为输入列表的每个元素检查第二个列表是否包含它。如果没有,请将其添加到第二个列表。这通过了您在原始 post:
中提到的测试用例import java.util.*;
public class MyClass {
public static void main(String args[]) {
List<Integer> test = new ArrayList<>();
List<Integer> noduplicates = new ArrayList<>();
test.add(1);
test.add(2);
test.add(1);
test.add(2);
test.add(1);
test.add(2);
test.add(3);
for (Integer i : test) {
if (!noduplicates.contains(i)) {
noduplicates.add(i);
}
}
System.out.println(noduplicates);
}
}
HashSet can do this since you cant put duplicate elements to HashSet, all you need to do is put your array into HashSet.
List<Integer> arr2 = new ArrayList<>(new HashSet<>(arr));
一个简单的方法是
将数组的数字以相反的顺序添加到
LinkedHashSet
以仅保留唯一数字并保留每个唯一数字的最后一个条目。从
LinkedHashSet
中创建一个List
。使用
Collections#reverse
反转List
。import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { Set<Integer> set = new LinkedHashSet<>(); int[] arr = { 7, 7, 4, 5, 7, 2, 7 }; // Add the numbers of the array in reverse order to a LinkedHashSet in order to remove // duplicates as well as to preserve the last entry of each unique number. for (int i = arr.length - 1; i >= 0; i--) { set.add(arr[i]); } // Create a List out of the LinkedHashSet List<Integer> list = new ArrayList<>(set); // Reverse the List Collections.reverse(list); // Display the result System.out.println(list); } }
输出:
[4, 5, 2, 7]