Posts

Showing posts with the label First Non Repeated Word in a String

Java Program to Find the First Non Repeating Word in a String

Image
In this section, we will show you two different ways to find first non repeating word in a given String in Java. 1.  Using For loop and Map 2. Using  Java 8 Streams Example 1. Using For loop and Map import java.util.LinkedHashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String str = "go python java go kotlin python kotlin" ; Map < String , Integer > map = new LinkedHashMap<>(); String [] words = str .split( " " ); for ( String word : words ) { if ( map .containsKey( word )) { map .put( word , map .get( word ) + 1 ); } else { map .put( word , 1 ); } } for ( Map . Entry < String , Integer > entry : map .entrySet()) { if ( entry .getValue()== 1 ) { System . out .println( entry .getKey()); break ; } }