英文:
For the below program , i want to print duplicate element which is 5,6,1 but when array is having some no 3 times its not providing correct output
问题
以下是翻译好的代码部分:
public static void main(String[] args) {
int[] data = {5, 6, 1, 6, 9, 5, 2, 1, 5, 6};
System.out.println(Arrays.toString(data));
ArrayList<Integer> dup = new ArrayList<>();
for (int index = 0; index < data.length; index++) {
if (dup.contains(data[index])) {
System.out.println("重复元素:" + data[index]);
} else {
dup.add(data[index]);
}
}
}
英文:
For the below program , i want to print duplicate element which is 5,6,1 but when array is having same no 3 times its not providing correct output
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] data = {5, 6, 1, 6, 9, 5, 2, 1, 5,6};
System.out.println(Arrays.toString(data));
ArrayList<Integer> dup = new ArrayList<>();
for(int index = 0; index < data.length; index++){
if(dup.contains(data[index])){
System.out.println("Duplicate Element : " + data[index]);
} else {
dup.add(data[index]);
}
}
}
答案1
得分: 4
你可以使用这个解决方案:
public static void main(String[] args) {
int[] data = {5, 6, 1, 6, 9, 5, 2, 1, 5, 6};
System.out.println(Arrays.toString(data));
Set<Integer> duplicates = new HashSet<>();
Set<Integer> duplicatesToShow = new LinkedHashSet<>();
for (int index = 0; index < data.length; index++) {
if (duplicates.contains(data[index])) {
duplicatesToShow.add(data[index]);
} else {
duplicates.add(data[index]);
}
}
System.out.println("Duplicate Element : " + duplicatesToShow.toString());
}
我们需要两个集合:一个用于跟踪重复项(duplicates),另一个用于稍后展示重复项(duplicatesToShow)。我们使用Set,因为这个集合只保留唯一的元素,没有重复项。
运行这段代码会产生以下输出:
[5, 6, 1, 6, 9, 5, 2, 1, 5, 6]
Duplicate Element : [6, 5, 1]
英文:
You can use this solution:
public static void main(String[] args) {
int[] data = {5, 6, 1, 6, 9, 5, 2, 1, 5,6};
System.out.println(Arrays.toString(data));
Set<Integer> duplicates = new HashSet<>();
Set<Integer> duplicatesToShow = new LinkedHashSet<>();
for(int index = 0; index < data.length; index++){
if(duplicates.contains(data[index])){
duplicatesToShow.add(data[index]);
} else {
duplicates.add(data[index]);
}
}
System.out.println("Duplicate Element : " + duplicatesToShow.toString());
}
We need to collections: one for tracking the duplicates (duplicates) and one for showing later the duplicates (duplicatesToShow). We use Set as this collections holds only unique elements and no duplicates.
Running this code yields:
[5, 6, 1, 6, 9, 5, 2, 1, 5, 6]
Duplicate Element : [6, 5, 1]
答案2
得分: 1
如果不需要保留顺序,可以直接使用集合。
List<Integer> data = new ArrayList<>(List.of(5, 6, 1, 6, 9, 5, 2, 1, 5, 6));
Set<Integer> noDupes = new HashSet(data); // 无重复项的项目
data.removeAll(noDupes); // 数据现在只包含重复项
Set<Integer> dupes = new HashSet(data); // 没有重复的重复项
英文:
If you don't need to retain the order, you can just use sets.
List<Integer> data = new ArrayList<>(List.of(5, 6, 1, 6, 9, 5, 2, 1, 5, 6));
Set<Integer> noDupes = new HashSet(data); // items without dupes
data.removeAll(noDupes); // data now contains only dupes
Set<Integer> dupes = new HashSet(data); // dupes without repetition
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论