Skip to content

Create longest_name.java #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions longest_name.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Java code for the above approach
import java.io.*;
import java.util.*;

class GFG
{

// Function to display longest names
// contained in the array
public static ArrayList<String> solve(String arr[],
int N)
{

// Edge Case
if (N == 0) {
ArrayList<String> temp
= new ArrayList<String>();
return temp;
}

// Initialize Max
int Max = arr[0].length();

// Create an arraylist res
ArrayList<String> res = new ArrayList<String>();

// Insert first element in res
res.add(arr[0]);

// Traverse the array
for (int i = 1; i < N; i++) {

// If string with greater length
// is found
if (arr[i].length() > Max) {
Max = arr[i].length();
res.clear();
res.add(arr[i]);
}

// If string with current max length
else if (arr[i].length() == Max) {
res.add(arr[i]);
}
}

// Return the final answer
return res;
}

// Driver Code
public static void main(String[] args)
{
String arr[] = { "GeeksforGeeks", "FreeCodeCamp",
"StackOverFlow", "MyCodeSchool" };

int N = arr.length;

// Function call
ArrayList<String> v = solve(arr, N);

// Printing the answer
for (String i : v) {
System.out.print(i + " ");
}
System.out.println();
}
}

// This code is contributed by Rohit Pradhan