Skip to content

Easy challenges - part 2 #2

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

Merged
merged 25 commits into from
Aug 16, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Third Greatest
  • Loading branch information
ardallie committed Aug 16, 2021
commit 80a4c89c5ec4afcec8e9f505cf46e371d8c2cac3
45 changes: 45 additions & 0 deletions src/easy/ThirdGreatest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package easy;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

/**
* Have the function ThirdGreatest(strArr) take the array of strings stored in strArr
* and return the third-largest word within it.
* ---
* So for example: if strArr is ["hello", "world", "before", "all"] your output
* should be world because "before" is 6 letters long, and "hello" and "world" are both 5,
* but the output should be world because it appeared as the last 5-letter word in the array.
* ---
* If strArr was ["hello", "world", "after", "all"] the output should
* be after because the first three words are all 5 letters long, so return the last one.
* The array will have at least three strings and each string will only contain letters.
*/
public class ThirdGreatest {

/**
* Third-Greatest function.
*
* @param strArr input array of strings
* @return the third-longest word
*/
private static String thirdGreatest(String[] strArr) {
Arrays.sort(strArr, Collections.reverseOrder());
Arrays.sort(strArr, Comparator.comparingInt(String::length));
return strArr[strArr.length - 3];
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = thirdGreatest(new String[]{"flowers", "decorate", "soul", "sleep"});
System.out.println(result1);
var result2 = thirdGreatest(new String[]{"surrounded", "darkness", "awakened", "within"});
System.out.println(result2);
}

}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy