简体   繁体   English

如何仅使用for循环和if在java中只打印一次重复值

[英]How to print duplicated value only once each using only for loop and if in java

    int[] numbers = {3, 2, 5, 11, 7, 10, 11, 3, 15, 11, 17, 10, 5};
    int count = 0;
    boolean dup = false;

    System.out.println("arrays value");
    for (int n : numbers ) {
        System.out.print(n +" ");
    }
    
    System.out.println("\n\nDuplicated value on arrays: ");
    for (int a = 0 ; a < numbers.length ; a++ ) {
        for (int b = a + 1 ; b < numbers.length ; b++ ) {
            if (numbers[a] == numbers[b]) {
                count = numbers[a];
                dup = true;
            }
        }
        
        if (dup) {
            System.out.print(count +" ");
            dup = false;
            count = 0;
        }
    }

I want to print duplicated value only once each using only for loop and if我想只打印一次重复的值,每个只使用 for 循环和 if

this output will print 3 5 11 10 11, how do I make only 3 5 11 10.此输出将打印 3 5 11 10 11,我如何仅生成 3 5 11 10。

For this it is smart to use the data structure set which is a collecion that do not allow duplicates.为此,使用不允许重复的集合的数据结构是明智之举。 This means that whatever the number of same values you add to the set only one will be stored.这意味着无论您添加到set的相同值的数量是多少,都只会存储一个。 Now you can simply write现在你可以简单地写

Set<Integer> mySet = new HashSet<>(Arrays.asList(numbers));
for(int number : mySet) {
    System.out.println(number);
}

If you only want to print the values that are duplicates and not the values that only exist once (your question is not entirely clear) you may do something like this instead如果您只想打印重复的值而不是只存在一次的值(您的问题并不完全清楚),您可以这样做

Set<Integer> mySet = new HashSet<>();
for(int number : numbers) {
   if(!mySet.add(number)) { //the set method add(e) returns false if e is a duplicate i.e. can not be added to the set
      System.out.print(duplicate + " ");
   }
}

If you want with for loop如果你想要 for 循环

import java.util.ArrayList;

public class stackov {
    public static void main(String[] args) {
        int[] numbers = {3, 2, 5, 11, 7, 10, 11, 3, 15, 11, 17, 10, 5};
        int count = 0;
        boolean dup = false;

        System.out.println("arrays value");
        for (int n : numbers) {
            System.out.print(n + " ");
        }
        ArrayList<Integer> integers = new ArrayList<>();
        System.out.println("\n\nDuplicated value on arrays: ");
        for (int i : numbers) {
            if (!integers.contains(i)) {
                integers.add(i);
            }
        }

        for (int x : integers) {
            System.out.print(x + " ");
        }
    }

} }

I assume that you are allowed multiple for-loops and the use of data structures.我假设您可以使用多个 for 循环和数据结构。

Iterate over the values and accumulate the count the occurrences of each value in a Map.迭代这些值并累积 Map 中每个值的出现次数。 Then iterate over the Map and output the values with a count > 1.然后迭代 Map 并输出计数 > 1 的值。

public static void main(String[]args) {
  int[] values = {...the values...};
  Map<Integer,Integer> map = new HashMap<>();
  for (int value : values) {
    if (map.containsKey(value)) {
      map.put(value, 1 + map.get(value));
    } else {
      map.put(value, 1);
    }
  }
  for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
    if (entry.getValue() > 1) {
      System.out.printf("Value %d was duplicated", value);
    }
  }
}

You need to mark all values that have been printed out, using a boolean array (instead of just a single boolean).您需要使用布尔数组(而不仅仅是单个布尔值)标记已打印的所有值。 Try something like:尝试类似:

        System.out.println("\n\nDuplicated value on arrays: ");
        boolean[] dup = new boolean[1000];
        for (int a = 0 ; a < numbers.length ; a++ ) {
            if (dup[numbers[a]] == false) {
                System.out.print(numbers[a]);
                dup[numbers[a]] = true;
            }
        }

If you want to use 2 for loops (which is slower), then you need to only check for the duplicate value before this current index (the for with b should be for (int b = 0; b < a; b++) )如果你想使用 2 个 for 循环(这比较慢),那么你只需要在这个当前索引之前检查重复值(for with b 应该是for (int b = 0; b < a; b++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM