Code Logo

Queue Peek Front

Published at25 Jul 2026
Easy 0 views
Like0

Given an array representing a queue, return the front element (first element) without removing it. Return 0 if empty.

Example Input & Output

Example 1
Input
[10,20]
Output
10
Example 2
Input
[]
Output
0
Example 3
Input
[5]
Output
5
Example 4
Input
[7,8,9]
Output
7
Example 5
Input
[1,2,3]
Output
1

Algorithm Flow

Recommendation Algorithm Flow for Queue Peek Front
Recommendation Algorithm Flow for Queue Peek Front

Solution Approach

function solution(arr) {
  return arr.length ? arr[0] : 0;
}

Best Answers

java
class Solution {
    public int solution(int[] nums) {
        return nums.length>0?nums[0]:0;
    }
}