iOS Dev India
Learn
Roadmap
Skills
Swift DSA
Build
Studio
Sandbox
Tools
Agent Skills
Community
Communities
Events
Library
Articles
Resources
Jobs
Create Profile
Home
Learn
Roadmap
Skills
Swift DSA
Build
Studio
Sandbox
Tools
Agent Skills
Community
Communities
Events
Library
Articles
Resources
Jobs
Create Profile
Challenges
/
Last Element
·
Step 7/100 · Hello Arrays
← Range Sum
Flip Signs →
Description
Hint
Last Element
L1
Easy
Arrays & Hashing
Concept
Array indexes run from
0
through
count - 1
, so the final element is always at
count - 1
. Handling the empty array is a skill on its own.
Return the last integer in
nums
. If the array is empty, return
-1
.
Examples
▸
nums = [1, 2, 3]
→ 3
▸
nums = [7]
→ 7
▸
nums = []
→ -1
Progressive Hints
Hint 1 · Nudge
The last spot of an array sits right before its size.
Hint 2 · Plan
First handle the empty case by returning -1. Otherwise return the element at index size minus 1.
Hint 3 · Approach
If the array is empty, return -1. Otherwise return nums[nums.length - 1].
Reveal the next hint
All hints are out. Take a breath and give it a shot.
Swift
Java
Solution.swift
Tests.swift (Read Only)
Run
Output
// Run your code to see the output here.
Press
ESC
to close.
Start typing to search...