Computer scienceProgramming languagesJavaWorking with dataCollectionsCollection implementationsThe Collection hierarchy implementations

ArrayDeque

Max value

Report a typo

You are given a list of integers. Your task is to write a function in Java that uses an ArrayDeque to find the maximum element in the list.

Input: The function takes a single argument—a List of integers list.

Output: The function should return the maximum integer in the list.

Sample Input 1:

3
1
2
3

Sample Output 1:

3
Write a program in Java 25
import java.util.*;

public class Main {
public static int findMax(List<Integer> list) {
Deque<Integer> deque = new ArrayDeque<>(list);
int max = Integer.MIN_VALUE;

while (!deque.isEmpty()) {
...
max = Math.max(max, current);
}
return max;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> l = new ArrayList<>();
for (int i = 0; i < n; i++) {
l.add(scanner.nextInt());
}
System.out.println(findMax(l));
}
}

Create a free account to access the full topic