-1

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?

Markus Mo
  • 17
  • 2
  • 2
    You could use a [map](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html) where the key is the string and the value is the count. – 001 Nov 16 '20 at 14:42
  • 1
    Check out the answers to [this question](https://stackoverflow.com/questions/8098601/java-count-occurrence-of-each-item-in-an-array). (May be duplicate, even though this question is worded differently) – maloomeister Nov 16 '20 at 14:46

2 Answers2

0

When you do your scan for 'how often does this thing occur in this array', if you find an occurrence with an index that is lower than your index, the loop for that lower index would have already printed this occurrence.

Thus, in that case, don't print anything. In terms of your code, instead of doing count++, do instead: Check if i2 is lower than i. If yes, do something to ensure that no printing occurs, otherwise, don't do that (tip: You can make a new local variable. Perhaps of type boolean?).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

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

    }
kwkw
  • 343
  • 1
  • 3
  • 9
  • 1
    That worked! To answer your question - I'm a beginner, and this is a training exercise for me. I tried solving the problem using multiple arrays, and it got quite messy indeed. This was very helpful, now I know I need to learn how to use hashmaps. Thanks for your help! – Markus Mo Nov 16 '20 at 16:34