Finding the third largest string in a given array in Java

In this tutorial,we get to find third largest string in a given array. Consider a string array from which third highest string must be displayed and if there are multiple strings with the same size then consider the last appeared string. This program would be helpful in understanding data structures like TreeMap. Before diving into the program we need to understand about TreeMap, HashMap, Scanner.

Finding the third largest string in a given array

There are certain constraints for this problem. If there are multiple third largest strings Then consider the last appeared string. For example, consider :

{“Hello”,”World”,”Codes”,”Hi”}

In the above situation, the output would be Codes.

The following code uses a Scanner which helps us to take input from user. After the size is received we create an array of strings and take inputs from user for the strings.

Scanner scan=new Scanner (System.in);
int size=scan.nextInt();
String[]array=new String[n];
for(int i=0;i<n;i++)
  array[i]=scan.next();

Output:

5

Hello World  code speedy hey.

Following code initializes a TreeMap which is basically a sorted HashMap.

for further guidance.

https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html#method.summary

Working with HashMap (Insertion, Deletion, Iteration) in Java

TreeMap<Integer,ArrayList<String>>hash=new TreeMap<>();

The Following code contains the main core of the problem.  It finds the length of the string which is the key in the considered treemap.

If the key found it retrieves the list associated as value. Then it adds the string into that list . If it is not found then it adds a new key associated with the length into out data structure.

       for(int i=0;i<size;i++)
	{
	   String a=array[i];
	   int length=a.length();
	   if(hash.containsKey(length))
               {
		ArrayList<String> temp=hash.get(length);
		temp.add(a);

	       }
	   else
               {
	  	ArrayList<String>temp=new ArrayList<>();
		temp.add(a);
		hash.put(len,temp);
         	}

	}

The following code specifies the retrieval of data from a TreeMap.

       for(Map.Entry element:hash.entrySet())
	{
		int key=(int)element.getKey();
		ArrayList<String> temp=hash.get(key);

		f=f+temp.size();
		if((n-f)<3)
		{
			System.out.print(temp.get(temp.size()-1));
			break;
		}

	}

Complete code is given below:

import java.util.*;
class third
{
  public static void main(String[] args)
   {
   	Scanner sc=new Scanner (System.in);
   	int size=sc.nextInt();
   	String[]arrray=new String[size];
   	for(int i=0;i<n;i++)
   		arrray[i]=sc.next();
   	TreeMap<Integer,ArrayList<String>>hash=new TreeMap<>();
   	for(int i=0;i<size;i++)
   	{
   		String a=arrray[i];
   		int len=a.length();
   		if(hash.containsKey(len)){
   		ArrayList<String> temp=hash.get(len);
   		temp.add(a);

   	}
   	else{
   		ArrayList<String>temp=new ArrayList<>();
   		temp.add(a);
   		hash.put(len,temp);
   	}

   	}
   	int f=0;
  
   	for(Map.Entry element:hash.entrySet())
   	{
   		int key=(int)element.getKey();
   		ArrayList<String> temp=hash.get(key);
   	//	g=f;
   		f=f+temp.size();
   		if((n-f)<3)
   		{
   			System.out.print(temp.get(temp.size()-1));
   			break;
   		}

   	}
   }

}

 

input:

6

Hello This Is Code Speedy Website

Output:

Hello

Leave a Reply

Your email address will not be published. Required fields are marked *