Skip to content

Commit 80a4c89

Browse files
committed
Third Greatest
1 parent dfd840a commit 80a4c89

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/easy/ThirdGreatest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package easy;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
7+
/**
8+
* Have the function ThirdGreatest(strArr) take the array of strings stored in strArr
9+
* and return the third-largest word within it.
10+
* ---
11+
* So for example: if strArr is ["hello", "world", "before", "all"] your output
12+
* should be world because "before" is 6 letters long, and "hello" and "world" are both 5,
13+
* but the output should be world because it appeared as the last 5-letter word in the array.
14+
* ---
15+
* If strArr was ["hello", "world", "after", "all"] the output should
16+
* be after because the first three words are all 5 letters long, so return the last one.
17+
* The array will have at least three strings and each string will only contain letters.
18+
*/
19+
public class ThirdGreatest {
20+
21+
/**
22+
* Third-Greatest function.
23+
*
24+
* @param strArr input array of strings
25+
* @return the third-longest word
26+
*/
27+
private static String thirdGreatest(String[] strArr) {
28+
Arrays.sort(strArr, Collections.reverseOrder());
29+
Arrays.sort(strArr, Comparator.comparingInt(String::length));
30+
return strArr[strArr.length - 3];
31+
}
32+
33+
/**
34+
* Entry point.
35+
*
36+
* @param args command line arguments
37+
*/
38+
public static void main(String[] args) {
39+
var result1 = thirdGreatest(new String[]{"flowers", "decorate", "soul", "sleep"});
40+
System.out.println(result1);
41+
var result2 = thirdGreatest(new String[]{"surrounded", "darkness", "awakened", "within"});
42+
System.out.println(result2);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)
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