计算数组中的重复值

时间:2015-09-12 08:41:28

标签: java

我想编写一个程序来计算数组中的重复项。如果两个相同的数字,代码可以工作。但是,如果有三个或更多相同的数字,则会出错。我该怎么做呢?

public class Duplicate
    {
      public static void main(String[] args)
      {
        int[] list = new int[]{1,2,3,4,5,6,7,8,8,8,9,10};

        int sum = 0;
        for(int count=1; count<list.length; count++)
        {
          if(list[count-1]==list[count])
          {
            sum = list[count-1] + list[count];
            System.out.println("Duplicate found: " + list[count] + " " + "Sum of the duplicate value is " +sum);
          }
        }
      }
    }

3 个答案:

答案 0 :(得分:8)

这是一种Java-8风格的功能方法:

int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 };

// Create a Stream<Integer> from your data
IntStream.of(array)
         .boxed()

// Group values into a Map<Integer, List<Integer>>
         .collect(Collectors.groupingBy(i -> i))

// Filter out those map values that have only 1 element in their group
         .entrySet()
         .stream()
         .filter(e -> e.getValue().size() > 1)

// Print the sum for the remaining groups
         .forEach(e -> {
             System.out.println(
                 "Duplicates found for : " + e.getKey() +
                 " their sum being : " + e.getValue()
                                          .stream()
                                          .collect(Collectors.summingInt(i -> i)));
         });

对于您的输入,这会产生:

Duplicates found for : 8 their sum being : 24

这个解决方案的好处在于它适用于无序int[]。例如。为...

int[] array = new int[] { 1, 10, 3, 2, 3, 4, 5, 8, 6, 7, 8, 8, 8, 9, 10 };

输出将是......

Duplicates found for : 3 their sum being : 6
Duplicates found for : 8 their sum being : 32
Duplicates found for : 10 their sum being : 20

答案 1 :(得分:4)

这将为您解决问题

public static void main(String[] args)
{
    int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 };

    int sum = 0;
    for (int j = 0; j < array.length; j++)
    {
        for (int k = j + 1; k < array.length; k++) 
        {
            if (k != j && array[k] == array[j])
            {
                sum = sum + array[k];
                System.out.println("Duplicate found: " + array[k] + " " + "Sum of the duplicate value is " + sum);
            }
        }
    }
}

希望它有所帮助!

答案 2 :(得分:4)

如果您不介意使用Javaslang(Java 8的集合库),这里有一个更简洁的解决方案:

int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 };

// javaslang.collection.List
List.ofAll(array)
        .groupBy(i -> i)
        .entrySet()
        .filter(e -> e.value.length() > 1)
        .forEach(e -> {
            System.out.println(
                    "Duplicates found for : " + e.key +
                    " their sum being : " + e.value.sum());
        });

正如所料,这产生了:

Duplicates found for : 8 their sum being : 24

同样,Lukas的回答是

int[] array = new int[] { 1, 10, 3, 2, 3, 4, 5, 8, 6, 7, 8, 8, 8, 9, 10 };

它屈服于

Duplicates found for : 10 their sum being : 20
Duplicates found for : 8 their sum being : 32
Duplicates found for : 3 their sum being : 6

请注意,此代码与Javaslang 2.0.0-SNAPSHOT一起运行,该版本即将发布。