Print all strings of maximum length from an array of strings | Java program to implement the above approach ; Function to find the length of the longest String from the given array of Strings ; Traverse the array ; Stores the length of current String ; Update maximum length ; Return the maximum length ; Function to print the longest Strings from the array ; Find the Strings having length equals to len ; Print the resultant vector of Strings ; Function to print all the longest Strings from the array ; Find the length of longest String ; Find and print all the Strings having length equals to max ; Driver Code
import java . util . * ; class GFG { static int maxLength ( String [ ] arr ) { int len = Integer . MIN_VALUE ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] . length ( ) ; if ( len < l ) { len = l ; } } return len ; } static void maxStrings ( String [ ] arr , int len ) { int N = arr . length ; Vector < String > ans = new Vector < String > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . length ( ) ) { ans . add ( arr [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } static void printStrings ( String [ ] arr ) { int max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } public static void main ( String [ ] args ) { String [ ] arr = { " aba " , " aa " , " ad " , " vcd " , " aba " } ; printStrings ( arr ) ; } }