Skip to content
Advertisement

Java method for checking for duplicate values in an array outputs new line for each duplicate value [closed]

I’ve created a method in java that checks (and counts) for duplicate values in an array:

   private static void checkDuplicates(String[] array)
    {
    int arrayLength = array.length;
        for (int i = 0; i < arrayLength; i++) {
            int count = 0;

            for (int i2 = 0; i2 < arrayLength; i2++) {
                if (array[i].equals(array[i2])) {
                    count++;
                }
            }
            System.out.println("The value " + array[i] + " appears " + count + " times in the array.");
            }
            }

Given the array {string1, string1, string2, string3, string1}, this produces the following output:

The value string1 appears 3 times in the array.
The value string1 appears 3 times in the array.
The value string2 appears 1 times in the array.
The value string3 appears 1 times in the array.
The value string1 appears 3 times in the array.

As you’ve probably understood by now, I don’t want the program to print one line for each occurence of the duplicate array element.

I’m sure this problem has an easy solution, but I’ve been working at this for hours and I can’t figure it out. Could someone point me in the right direction?

Advertisement

Answer

I am curious to know your goal. Is this a training exercise for you, where you are only allowed to use arrays and are you limited to your training so far?

I agree with previous answers, they suggest using a map. Using arrays to store your counts gets messy, because you need two corresponding arrays. These arrays will have a set length and will probably be too long. It will become messy with for example: String[] texts = new String[array.length]; int[] counts = new int[array.length]; A Map, for example a hashmap is recommended HashMap<String, Integer> Do not forget to import java.util.HashMap;

Your code could look like this(I tried to retain as much of your example)

public static void main(String[] args) {
        String[] duplicates = new String[]{"string1", "string1", "string2", "string3", "string1"};
        checkDuplicates(duplicates);
    }

    private static void checkDuplicates(String[] array)
    {
        HashMap<String, Integer> duplicateCounter = new HashMap<String, Integer>();

        for(int i=0; i<array.length; i++){
            if(duplicateCounter.containsKey(array[i])){
                //the HashMap already has an entry with this key, so 1 should be added
                //array[i] is of course the String, for example "string1"
                duplicateCounter.put(array[i], duplicateCounter.get(array[i])+1);
            }
            else{
                //the HashMap does not contain your string. A first entry has to be made
                duplicateCounter.put(array[i], 1);
            }
        }
        //Now print your HashMap
        for(String word:duplicateCounter.keySet()){
            System.out.println("The value " + word + " appears " + duplicateCounter.get(word)+ " times in the array.");
        }

    }

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement