0

Brand new to Java and I cannot seem to figure this out:

All I'm trying to do is print a duplicate string and the number of times it shows up in an array (not using hash tables or anything like that, just very simplistically).

Let's say for an array like this:

tempArray = {"dogs", "cats", "dogs", "dogs", "mice", "snakes", "cats"}

Here's my code thus far:

int flowerCount = 0;
for (int j = 0; j < tempArray.length - 1; j++) {
        for (int k = j + 1; k < tempArray.length; k++) {
              if( (tempArray[j].equals(tempArray[k])) && (j != k) ) {
                    System.out.println(tempArray[j]);
                    flowerCount++;
               }

         }

 }

Obviously this doesn't work, what am I doing wrong here? This seems like it should be so simple to do, but I can't get the nested loops and counter right.

14
  • 1
    Why does it obviously not work? What is the expected output, and what is the actual output? Commented Feb 6, 2016 at 4:13
  • As i stated, the expected output is "print a duplicate string and the number of times it shows up in an array ". All that it does currently is print the duplicated item an erroneous number of times Commented Feb 6, 2016 at 4:14
  • are you looking for a specific string that is known before the function runs, or are you trying to find all strings that are duplicated? so, in your example, should your output be "dogs"*3, "cats"*2? Commented Feb 6, 2016 at 4:14
  • You have a one-dimensional array. Having the inner for loop is over-complicating things. Commented Feb 6, 2016 at 4:15
  • 1
    I'd just sort the array and look for equal consecutive elements. Commented Feb 6, 2016 at 4:19

3 Answers 3

2

You can sort the array using Arrays.sort. This will put equal elements next to each other. Then you can simply iterate through the list with a while loop, looking for consecutive elements which are equal.

int i = 0;
while (i < arr.length) {
  int start = i;
  while (i < arr.length && arr[i].equals(arr[start])) {
    ++i;
  }
  int count = i - start;
  System.out.println(arr[start] + " " + count);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can do it with one loop only and print previous when element changes.
1

One easy way to count duplicates is to try to add them to a set. A set doesn't allow duplicates, so each time adding a string fails, it is because the string already exists in the set.

The add() method in a set returns a boolean, indicating if the add was successful or not. If the string you are trying to add is already in the set, the add will fail and the method will return false.

So something like:

HashSet<String> yourSet = new HashSet<>(); //Could be any kind of set, I'm just used to HashSets
int j = 0; j < tempArray.length - 1; j++) {
    if (yourSet.add(tempArray[j]) {
        //String was added succesfully, so it is not a duplicate.
    }  else {
        //String is duplicate.  Increment a duplicate counter for this string (and start at 2, if you want to include the initial occurence that is already in the set
    }
}

Comments

1

with array and for

String printed = "";
    for(String auxOne : tempArray){
        int CountRepeat = 0;
        for(String auxTwo : tempArray){
            if(auxOne.equals(auxTwo)){
                CountRepeat++;
            }
        }
        if(CountRepeat>1 && (printed.indexOf(auxOne)==-1)){
            printed += auxOne;
            System.out.println(auxOne + " : " + CountRepeat);
        }
    }

}

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.