Code Logo

Middle of List

Published at25 Jul 2026
Easy 0 views
Like0

Find the middle element of an array. For even length, return the second middle element.

Example Input & Output

Example 1
Input
[1,2,3,4]
Output
3
Explanation

Second middle for even

Example 2
Input
[1,2,3,4,5]
Output
3
Explanation

Middle of 5 elements

Example 3
Input
[1,2,3]
Output
2
Explanation

Three elements

Example 4
Input
[1,2]
Output
2
Explanation

Two elements

Example 5
Input
[1]
Output
1
Explanation

Single

Algorithm Flow

Recommendation Algorithm Flow for Middle of List
Recommendation Algorithm Flow for Middle of List

Solution Approach

function solution(arr) {
  return arr[Math.floor(arr.length / 2)];
}

Best Answers

java
class Solution {
    public int solution(int[] nums) {
        return nums[nums.length/2];
    }
}