Jon Calvert
Jon Calvert

Reputation: 1

I'm trying to retrun and int for the number of duplicate strings in an array, but I'm new at this

public int duplicateWords(){
        for (String word : wordStore) {
            if (word.equals(word)  ) {


            }
        }
    }

I already have my array set up but what do I compare my word val to?

Upvotes: 0

Views: 65

Answers (5)

funky
funky

Reputation: 313

I do not know if it is this what you are asking for. But in my method you can feed an Array of Strings and the method returns an int that gives you the number of strings that are in this array more than once.

import java.util.ArrayList;
import java.util.List;

public class testMe {

public static void main(String[] args) {

    String[] names = {"Brad", "Torben", "Julia", "Julia", "Ulli", "Thorsten", "Anna", "Thorsten", "Anna", "Anna", "Anna", "Anna"};
    int wordCount = duplicateWords(names);
    System.out.println(wordCount);
}

    public static int duplicateWords(String[] arrayOfWords){
        int count = 0;
        List<String> duplicatedWords = new ArrayList<String>();
        for(int i = 0; i < arrayOfWords.length; i++){
            String wordToCompare = arrayOfWords[i];
            for(int j = i + 1; j < arrayOfWords.length; j++){
                String everyOtherWordTemp = arrayOfWords[j];
                if(wordToCompare.equals(everyOtherWordTemp) && !duplicatedWords.contains(wordToCompare)){
                    duplicatedWords.add(arrayOfWords[i]);
                    count++;                    
                }
            }
        }
        return count;
    }
}

With my small testarray the method will return the Number 3. Since Julia, Thorsten and Anna are duplicated Strings in this array.

Upvotes: 0

Bahij.Mik
Bahij.Mik

Reputation: 1484

If you want to find the number of duplicates in an array then you can do this:

public static int duplicateWords(String[] words) {
        HashSet<String> duplicateSet = new HashSet<>();
        int total = 0;
        for(String word : words) {
            if(duplicateSet.contains(word)) {
                total++;
            } else {
                duplicateSet.add(word);
            }
        }
        return total;
    }

Upvotes: 0

devMohaned
devMohaned

Reputation: 145

Here's an example of duplication, it might help.

 String[] duplicates = {"A","B","C","D","E","F","G","G","H","H"}; // The duplicates are G & H in this case.

    public int duplicateWords(String searchedWord /* It can be any String */){
    int duplicateCount = 0;
            for (String currentWord : duplicates) {
                if (searchedWord.equals(currentWord)  ) {
                    duplicateCount++;
                }
            }
        return duplicateCount;
        }

Upvotes: 0

Robert Hajek
Robert Hajek

Reputation: 1

Take a look at the List method contains(), that will help you check if the word already exists in wordStore.

If you don't care about counting how many times each duplicate appears or which are duplicated, you could look into how to convert wordStore to a Set, which forces unique values, and check the difference in length to your original wordStore.

That should point you in the right direction, short of giving you an exact answer.

Upvotes: 0

boi yeet
boi yeet

Reputation: 86

It seems to me you don't have another value besides word. You might want to try making another int value, and then comparing those things to rerun your code multiple times. This is only a guess, and it would help if you provided me with the set of instructions for this assignment. Then I will send you the code and explain what they do.

Upvotes: -1

Related Questions