0
package array;

import java.util.Arrays;

public class learning_program1 {

    public static void main(String[] args) {

        int arr[]= {1,2,3,4,10,20,30,6,6,5,4,5,5,2};

        Arrays.sort(arr);
        for(int i = 0; i < arr.length; i++) 
        {   
            int count =0;
            int flag=0;
            for(int j=i+1; j<arr.length; j++)
            {
                while(arr[i] == arr[j])
                {
                    count++;
                    j++;
                    flag=1;
                }
                break;
            }
            if(flag==1)
            {
                System.out.println("the repeated values " + arr[i] + " is " +count);
            }


        }

    }
}

Output:

the repeated values 2 is 1

the repeated values 4 is 1

the repeated values 5 is 2

the repeated values 5 is 1

the repeated values 6 is 1

my question is i am getting the output but 5 is repeating twice

4
  • 1
    You can tackle this situation better by using a Map or a HashMap. Keep the digit in the array as a key inside Map or HashMap and its count as value. Commented Jun 4, 2018 at 8:30
  • yep, i tired by while(arr[i] == arr[j]) { count++; j++; flag=1; } break; i=j;(assinging duplicate values of last find and assign to i) but i am not getting the proper output Commented Jun 4, 2018 at 8:38
  • There is no need to sort the input Commented Jun 4, 2018 at 8:41
  • The current answers show good ways of tackling this problem, but you might find them a bit advanced if you are only just beginning to learn Java. You can still carry on with your solution using only arrays. Your error is in your inner for/while loop. You only need one loop there. Think carefully about the terminating condition. It may help if you do this manually first (using pen and paper) to see exactly what you need to do. Commented Jun 4, 2018 at 9:04

2 Answers 2

1

You can use Stream. First you have to group all elements in the given arr by it's value and counting them. Then, filter elements that appears more than once.

public static Map<Integer, Integer> findDuplicates(int[] arr) {
    Map<Integer, Long> map = Arrays.stream(arr)
                                   .boxed()
                                   .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    Map<Integer, Integer> res = new TreeMap<>();

    map.entrySet().stream()
       .filter(entry -> entry.getValue() > 1)
       .forEach(entry -> res.put(entry.getKey(), entry.getValue().intValue() - 1));

    return res;
}

In this case, your client code will be look like this:

int arr[] = { 1, 2, 3, 4, 10, 20, 30, 6, 6, 5, 4, 5, 5, 2 };
Map<Integer, Integer> map = findDuplicates(arr);
map.forEach((key, count) -> System.out.println("the repeated values " + key + " is " + count));

Outputs:

the repeated values 2 is 1
the repeated values 4 is 1
the repeated values 5 is 2
the repeated values 6 is 1

P.S. In case you hesitate to use Stream, the it is easy to do it without it, just rely on Set and Map:

public static Map<Integer, Integer> findDuplicates(int[] arr) {
    Set<Integer> values = new HashSet<>();
    Map<Integer, Integer> map = new TreeMap<>();

    for (int val : arr)
        if (!values.add(val))
            map.put(val, map.getOrDefault(val, 0) + 1);

    return map;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should change your method used to find duplicates. It's better to use HashMap. Try to implement it like this:

Map<Integer, Integer> findDuplicates(int[] arr) {
    Map<Integer, Integer> map = new HashMap<>();
    for(int i: arr) {
        if(map.containsKey(i)) {
            map.put(i, map.get(i)+1);
        } else {
            map.put(i, 1);
        }
    }

    return map;
}

Then just iterate through all keys in the map and check how many elements it has.

If you still want to use your code, then you can use another variable to keep list of numbers which where already processed. Your code checks 5 two times:

  1. 5, 4, 5, 5
  2. 4, 5, 5

So the output looks like it looks

1 Comment

Of course we can. Just use your code but just be sure that you don't check the same numbers several times. You can also change your array to List and use Collections class. Something like this if (Collections.frequency(iList, elem) > 1) {...} There are many different solutions to this problem, but HashMap is very simple and efficient.

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.