Skip to content

#023 First Element in an Array

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

023 First Element in an Array

Create a function that takes an array containing only values of the same type and return the first element.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/023 First Element in an Array.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function that takes an array containing only values of the same type and return the first element.
function getFirstValue(arr) {
    return arr[0];
}
import * as Test from '../testHelper.js';
Test.assertEquals(getFirstValue([1, 2, 3]), 1)
Test.assertEquals(getFirstValue([80, 5, 100]), 80)
Test.assertEquals(getFirstValue([-500, 0, 50]), -500)
Test.assertEquals(getFirstValue([5, 2, 3]), 5)
Test.assertEquals(getFirstValue([75675, 5, 100]), 75675)
Test.assertEquals(getFirstValue([-52320, 0, 50]), -52320)

Historical reference notes

Cross-language explanation preserved from the study materials

Reference file: #023 First Element in an Array.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.

# [First Element in an Array](https://edabit.com/challenge/HEWXeauE6aW2dh4fg)
Published by [Nomore](https://edabit.com/user/p6ESyNjSyY6QbwMDW)

arrays language_fundamentals

Create a function that takes an array containing only values of the same type and return the first element.

`GetFirstValue([5, 6, 7]) ➞ 5`

`GetFirstValue(["Semiramis", "Gaia", "Hypatia"]) ➞ "Semiramis"`

`GetFirstValue([true, false, true]) ➞ true`

The first element in an array always has an index of 0.

### Time Complexity:
The time complexity of `getFirstValue` is _O(1)_, or constant time. Accessing the first element of an array (`arr[0]`) is a direct operation and does not depend on the size of the array. Therefore, it always completes in constant time.

### Space Complexity:
The space complexity is also _O(1)_. The function does not use any additional memory that scales with the input size; it simply returns a reference to the first element of the input array.

![flowchart](https://diagrams.helpful.dev/d-r2/d-r2:4WuoiirK)

* `Index` methods showed the fastest execution times, with consistently achieving the shortest mean times, ranging from **0.89 to 1.07 ns**.
* `First` methods averaged around **34–39 ns**.
* `FirstOrDefault` methods exhibited the longest execution times, with means ranging from **40.95 to 64.35 ns**. These methods performed around 40-60 times slower than `Index`.

Clone this wiki locally