-1
public static void checkMultiple(int a[]){

    //will use this count variable later
    //int[] count = new int[10];

    //first scan over the array
    for (int i = 0; i < a.length; i++)
    {
    //seeded loop to check against 1st loop    
        for (int j = 0; j < i; j++)
        {    
            if (a[i] == a[j])
            {
            System.out.print(a[i] + " ");
            }
        }
    }
}

I'm having trouble counting repeated numbers in an integer array of 10 random numbers. I havent wrote the "count" function yet but the checkMultiple() will print out the numbers that are repeated. However, some of the time it prints correctly such as:

4 2 9 0 9 6 3 3 7 5

9 3

the first line being the whole array and the second the numbers that are repeated at least once in the array. But when there is more than two of a single integer, it counts every single one of that integer such as:

9 5 2 8 5 5 7 6 3 3

5 5 5 3

Any tips or advice would be much appreciated!

  • Do you want to stick with arrays? There are other data structures in java which would make this task easier. As in one of the answers mentioned a map would do the trick – Basti Mar 07 '18 at 19:16
  • Possible duplicate of [Java Array, Finding Duplicates](https://stackoverflow.com/questions/3951547/java-array-finding-duplicates) – JonathanDavidArndt Mar 07 '18 at 19:20

4 Answers4

0

Just use a hash map, if the key does not exist add it to the map with the value 1, if it does increase the value, then print the hash map.

A single iteration of the array will solve the problem.

Map<Integer, Integer> counter = new HashMap<>();

for (int i = 0; i < a.length; i++) {
    if (counter.containsKey(a[i])) {
        counter.put(a[i], counter.get(a[i]) + 1);
    } else {
        counter.put(a[i], 1);
    }
}

Iterator it = counter.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry) it.next();

    for (int i = 0; i < pair.getValue(); ++i) {
        System.out.print(pair.getKey() + " ");    
    }

    // or you could just print the number and how many times it was found
    System.out.println(pair.getKey() + " " + pair.getValue());    

    it.remove();
}
LZR
  • 948
  • 10
  • 28
0

It looks like as you are looping through and then immediately outputting the results.

This prevents the program from comparing what's being currently parsed and what has already been been parsed and counted.

I would pass an empty array into the first FOR loop and instead of "System.out.print," store the number in the passed-in array. You can then compare the values that have already been parsed against the value currently being parsed to see if it has already been counted.

When the outside FOR loop exits, output the array that was originally passed in empty (and now has a record of every duplicate in the initial array)

0

You are counting the number of duplicate instances. Your second example has three pairs of duplicate 5's. That's why you see the 5 repeated three times. Only output unique duplicate results.

Rickest Rick
  • 1,519
  • 1
  • 15
  • 28
-1

try this

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class BirthdayCake {

    private static void printcake(int n,int[] arr){
        //Map<Integer,Integer> ha = new HashMap<Integer,Integer>();
        int temp = arr[0],max=0, count=0;
        /*for(int i=1;i<n;i++){
            max = arr[i];
            if(max<=temp){
                temp=max;
                count++;
                break;
            }
            else{
                max = arr[i];
                count++;
                break;
            }

        }*/
        Arrays.sort(arr);
        for(int i:arr){
            System.out.println(i);
        }
        System.out.println("max:" +max);
        max = arr[arr.length-1];
        for(int i=0;i<n;i++){
            if(arr[i]==max){
                count++;
            }

        }

        System.out.println("count:" +count);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Entere thye array size:");
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++){
            arr[i] = sc.nextInt();
        }
         printcake(n,arr);
    } 

}