Home > Mobile >  How do I find if a word is repeated in a string? Java
How do I find if a word is repeated in a string? Java

Time:11-09

In my program, the user is prompted to enter a string of words, but I can’t figure out how the logic of how the program could see if the same word is repeated. The scanner is imported.

Scanner console = new Scanner(System.in);
System.out.print(”Enter a string of words: “);
String str = console.nextLine();

//this is where I have no idea what to do

CodePudding user response:

tl;dr

Arrays
        .stream(
                "tacocat backwards is tacocat"
                        .split( " " )
        )
        .collect(
                Collectors.groupingBy(
                        Function.identity() ,
                        Collectors.counting()
                )
        )
        .entrySet()
        .stream()
        .filter(
                entry -> entry.getValue() > 1
        )
        .collect(
                Collectors.toMap(
                        entry -> entry.getKey() ,
                        entry -> entry.getValue()
                )
        )

{tacocat=2}

Details

Define some example input.

String input = "tacocat backwards is tacocat";

Split that input string into an array of words.

String[] words = input.split( " " );

Count the occurrences of each word, storing the count in a map.

Map < String, Long > wordCount =
        Arrays
                .stream( words )                       // Make a stream of our array of `String` objects.
                .collect(
                        Collectors.groupingBy(
                                Function.identity() ,  // Map’s key: Get the word itself.
                                Collectors.counting()  // Map’s value: Count occurrences of that word.
                        )
                );

Dump to console.

System.out.println( wordCount );

{tacocat=2, is=1, backwards=1}

We can further reduce that map to include only the words that are duplicates, if we don't care about words with a count of one.

Map < String, Long > dupCount =
        wordCount
                .entrySet()
                .stream()
                .filter(
                        entry -> entry.getValue() > 1
                )
                .collect(
                        Collectors.toMap(
                                entry -> entry.getKey() ,
                                entry -> entry.getValue()
                        )
                );

Dump to console.

System.out.println( dupCount );

{tacocat=2}

See the code above run at Ideone.com.

We can combine those lines into one. Not that I recommend doing so.

Map < String, Long > dupCount =
        Arrays
                .stream(
                        "tacocat backwards is tacocat"
                                .split( " " )
                )
                .collect(
                        Collectors.groupingBy(
                                Function.identity() ,
                                Collectors.counting()
                        )
                )
                .entrySet()
                .stream()
                .filter(
                        entry -> entry.getValue() > 1
                )
                .collect(
                        Collectors.toMap(
                                entry -> entry.getKey() ,
                                entry -> entry.getValue()
                        )
                );

CodePudding user response:

    Set<String> words = new HashSet<String>();
    Scanner console = new Scanner(System.in);
    
    System.out.print("Enter a string of words: ");
    String allWords = console.nextLine();
    String[] singleWords = allWords.split(" ");
    
    for(int i = 0; i < singleWords.length; i  ) 
    {
        if(words.contains(singleWords[i]))
            System.out.println("Duplicate: "   singleWords[i]);
        else
            words.add(singleWords[i]);
    }
  • Related