
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Find the first repeated word in a string in Java
To find the first repeated word in a string in Java, the code is as follows −
Example
import java.util.*; public class Demo{ static char repeat_first(char my_str[]){ HashSet<Character> my_hash = new HashSet<>(); for (int i=0; i<=my_str.length-1; i++){ char c = my_str[i]; if (my_hash.contains(c)) return c; else my_hash.add(c); } return '\0'; } public static void main (String[] args){ String my_str = "thisisasampleonlysample"; char[] my_arr = my_str.toCharArray(); System.out.println("The first repeating character in the string is :"); System.out.println(repeat_first(my_arr)); } }
Output
The first repeating character in the string is : I
A class named Demo contains a function named ‘repeat_first’, that takes a character string as a parameter. It creates a new hash set and iterates over the string and checks if the character in the string is equal to a specific character.
If yes, then the character is returned, otherwise, the character is added to the hash set. This way, the second time a word is found, it is added to the hash set, and this becomes the first word, that was in the string more than once. In the main function, the string is defined and a character array is defined. The function ‘repeat_first’ is called on this character array. The relevant array is displayed on the console.
- Related Articles
- Find the first repeated word in a string in Python?
- Find the first repeated word in a string in C++
- Find the first repeated word in a string in Python using Dictionary
- Find the second most repeated word in a sequence in Java
- Find the first repeated character in a string using C++.
- Find repeated character present first in a string in C++
- Find First and Last Word of a File Containing String in Java
- Find the first maximum length even word from a string in C++
- Java Program to Capitalize the first character of each word in a String
- How to print the first character of each word in a String in Java?
- Getting first letter of each word in a String using regex in Java
- Find frequency of each word in a string in Java
- How to replace only the first repeated value in a string in MySQL
- C++ program to find Second most repeated word in a sequence
- How to Find the Most Repeated Word in a Text File using Python?

Advertisements