Aurora_Titanium
Aurora_Titanium

Reputation: 472

Calculating the Duplicate Values in an Array

I am suppose to write a program to calculate duplicates in an array. The code works if the are two of the same number. However, there is an error if there are three or more of the same number. How do I go about it?

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);
          }
        }
      }
    }

Upvotes: 3

Views: 4230

Answers (3)

Daniel Dietrich
Daniel Dietrich

Reputation: 2272

If you don't mind to use Javaslang, a collection library for Java 8, here is more a concise solution:

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());
        });

As expected, this yields to:

Duplicates found for : 8 their sum being : 24

And, likewise to Lukas' answer, for

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

it yields to

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

Please note that this code runs with Javaslang 2.0.0-SNAPSHOT, which is released soon.

Upvotes: 4

Lukas Eder
Lukas Eder

Reputation: 220762

Here's a Java-8-style, functional approach:

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)));
         });

For your input, this yields:

Duplicates found for : 8 their sum being : 24

The nice thing about this solution is that it works for unordered int[] just the same. E.g. for...

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

The output will be...

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

Upvotes: 8

volkanozkan
volkanozkan

Reputation: 285

This will do the trick for you

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);
            }
        }
    }
}

Hope it helps!

Upvotes: 4

Related Questions