3

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);
          }
        }
      }
    }
3
  • 2
    What exactly is "calculating duplicate numbers"? Commented Sep 12, 2015 at 8:45
  • Is the input array always sorted? What is the output you're expecting? Commented Sep 12, 2015 at 8:51
  • array will be always sorted?? Commented Sep 12, 2015 at 8:52

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

Comments

4

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!

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.