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

huangapple go评论108阅读模式
英文:

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&lt;Integer&gt; dup = new ArrayList&lt;&gt;();
    		for(int index = 0; index &lt; data.length; index++){
    		    if(dup.contains(data[index])){
    		        System.out.println(&quot;Duplicate Element : &quot; + 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&lt;Integer&gt; duplicates = new HashSet&lt;&gt;();
    Set&lt;Integer&gt; duplicatesToShow = new LinkedHashSet&lt;&gt;();
    for(int index = 0; index &lt; data.length; index++){
      if(duplicates.contains(data[index])){
        duplicatesToShow.add(data[index]);
      } else {
        duplicates.add(data[index]);
      }
    }
    System.out.println(&quot;Duplicate Element : &quot; + 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&lt;Integer&gt; data = new ArrayList&lt;&gt;(List.of(5, 6, 1, 6, 9, 5, 2, 1, 5, 6));

Set&lt;Integer&gt; noDupes = new HashSet(data); // items without dupes
data.removeAll(noDupes); // data now contains only dupes
Set&lt;Integer&gt; dupes = new HashSet(data); // dupes without repetition

huangapple
  • 本文由 发表于 2020年10月1日 20:30:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64155460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定