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
/
Array Average
·
Step 13/100 · Hello Arrays
← Temperature Filter
Contains Value →
Description
Hint
Array Average
L1
Easy
Arrays & Hashing
Concept
An average is the sum divided by the size. Dividing integers produces a fractional value, so convert to
Double
before dividing.
Return the average of the integers in
nums
as a
Double
. If the array is empty, return
0
.
Examples
▸
nums = [1, 2, 3, 4]
→ 2.5
▸
nums = []
→ 0
▸
nums = [-2, 2]
→ 0
Progressive Hints
Hint 1 · Nudge
Sum everything up, then share it out equally among the items.
Hint 2 · Plan
Handle the empty array by returning 0. Otherwise add all elements, divide by the count using decimal math, and return the result.
Hint 3 · Approach
If empty, return 0. total = sum of all n. Return total divided by the count, as a decimal.
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...