What are the ways by which duplicate word in a String can be detected?
e.g. “this is a test message for duplicate test” contains one duplicate word test.
Here, the objective is to detect all duplicate words which occur in a String.
Use of regular expression is preferable to achieve the goal.
The following Java code resolves the problem of detecting duplicates from a String. There should not be any problem if the duplicate word is separated by newline or punctuation symbols.
The output of the code will be:
Here, m.group(1) statement represents the String matched against 1st group of Pattern [here, it’s (\\w+)].