Skip to content

Commit f094820

Browse files
committed
Add largest element in an array examples
1 parent 1a5a314 commit f094820

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Visit coderolls.com
1+
# Visit [coderolls.com](https://coderolls.com)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coderolls.JavaPrograms;
2+
/**
3+
* A Java program to find the largest number in an array
4+
* using for loop iteration.
5+
*
6+
* @author coderolls.com
7+
*
8+
*/
9+
public class LargestElementInArray {
10+
11+
public static void main(String[] args) {
12+
int[] arr = {2, 5, 9, 8, 11};
13+
14+
int largestElement = getLargest(arr);
15+
System.out.println("Largest element in an array 'arr' is :"+ largestElement);
16+
17+
}
18+
19+
private static int getLargest(int[] arr) {
20+
int n = arr.length;
21+
int largest = arr[0];
22+
23+
for(int i=0; i<n; i++) {
24+
// if number at index i is bigger than the current largest number,
25+
// copy it to 'largest' int variable
26+
if(arr[i]>arr[0]) {
27+
largest = arr[i];
28+
}
29+
}
30+
return largest;
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderolls.JavaPrograms;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* A Java program to find the largest number in an array
7+
* using Arrays.sort().
8+
*
9+
* Arrays.sort() sort the array in natural sorting order
10+
*
11+
* @author coderolls.com
12+
*
13+
*/
14+
public class LargestElementInArrayUsingArrays {
15+
16+
public static void main(String[] args) {
17+
int[] arr = {2, 5, 9, 8, 11};
18+
19+
int largestElement = getLargest(arr);
20+
System.out.println("Largest element in an array 'arr' using Array.sort() is :"+ largestElement);
21+
22+
}
23+
24+
private static int getLargest(int[] arr) {
25+
Arrays.sort(arr);
26+
return arr[arr.length-1];
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.coderolls.JavaPrograms;
2+
3+
import java.util.Arrays;
4+
/**
5+
* A Java program to find the largest number in an array
6+
* using Java 8 Streams API.
7+
*
8+
* max() returns the maximum element of this stream
9+
*
10+
* @author coderolls.com
11+
*
12+
*/
13+
public class LargestElementInArrayUsingStream {
14+
15+
public static void main(String[] args) {
16+
int[] arr = {2, 5, 9, 8, 11};
17+
18+
int largestElement = getLargest(arr);
19+
System.out.println("Largest element in an array 'arr' using Java 8 Streams API is :"+ largestElement);
20+
}
21+
22+
private static int getLargest(int[] arr) {
23+
int largest = Arrays.stream(arr).max().getAsInt();
24+
return largest;
25+
}
26+
}

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