find out the first non-repeated word in the file

Problem # 1: Suppose you have a plain text file. Write a simple java program to find out the first non-repeated word in the file.

Note:  this problem is given by Therap Java Fest in their facebook page.


Finding solution for this problem is quite easy.  We have two parts in this program. First one, we have to read text from file and then find the first non-repeated word in the text . So we need to write two methods.  Say they are, readTextFromFile() and findFirstNonRepeatedWord().

First method is pretty straightforward. It will take a text file name with its path as string.  And then read the text file and return as a String.  So first let’s create a file object and put the file name of its constructor argument.  And then create another object of BufferedRader and put a FileReader object argument to its constructor and FileReader ’s constructor takes the File object as its constructor argument.

There are some benefits using BufferedReader. It can speed up IO quite a bit. Using its readLine()  method, we can read a single line at time in the loop rather than read character one by one.

As this method reads all the text and returns as a single string, so a lot of concatenation is required for adding every single line to a String which is returned by this method. String is an immutable object. So if the file is large, here its needed a lot of concatenation which is costly for immutable object. So it’s better to use StringBuilder.  StringBuilder is mutable object and it’s much faster.

Now let’s talk about second method.

We have String now. Let’s split it first using split method in string. Split method takes a string regex and returns an array of String. In our case, let’s split our string using single space. We need a map now. Map is an interface provided by Java collection Framework. Map cannot contain duplicate keys; each key can map to at most one value. Here map has string key and integer value. The key is our word and integer is a counter.

We use a foreach loop for the array of string. Inside the loop we use an if-checking. We find integer value from the map using string word as a value. If the word is found the map, the get method returns the value, otherwise it return null. If returns value is null which means no value for that word in the map then we insert a ‘1’. If there is already a value for that word, increment the value by 1.

After the first part is done, we use for-each loop in the String array which we first splitted from the parameter string. Then look up every word in the map.  If there is a 1, return that word because it is the first non-repeating word. If none of words have value ‘1’ then returns null which means there are no non-repeating words in the file.

Source Code:

import java.util.Hashtable;
import java.util.Map;

public class FirstNonRepeatedWordFinder {

public static String readTextFromFile(String fileName)
throws FileNotFoundException {
StringBuilder builder = new StringBuilder();
File file = new File(fileName);
// check if file is exist or not, if not exist throw a Exceptions
if (!file.exists()) {
throw new FileNotFoundException();

BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = reader.readLine()) != null) {
} catch (IOException e) {
return builder.toString();

public static String findNonRepeatedWords(String text) {

Map<String, Integer> map = new Hashtable<>();
Integer intgr = null;

String[] split = text.split(" "); // split this text using single space.

for (String str : split) {

intgr = map.get(str);
// If there is no value for that word in the
// map then insert a '1'
if (intgr == null) {
map.put(str, new Integer(1));
} else {
// else if there is already a value for that word, increment the
// value by 1
map.put(str, new Integer(intgr.intValue() + 1));
// /System.out.println(map);

for (String str : split) {
if (map.get(str).intValue() == 1) {
return str;

return null;

public static void main(String[] args) {
try {
.println("First Non Repeated Word is: "
+ findNonRepeatedWords(readTextFromFile("findNonRepeatedWord.txt")));
} catch (FileNotFoundException e) {

Download Source : CLICK HERE

Keep calm and carry on coding and smile.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s