Sum Array

Easy~5 min

Given an array of integers nums, return the sum of all elements.

This is your first real coding problem in the Big O section. The optimal solution is O(n) — you must visit every element exactly once.

Examples

Example 1
Input: nums = [1, 2, 3, 4, 5]
Output: 15
Explanation: 1 + 2 + 3 + 4 + 5 = 15
Example 2
Input: nums = [-1, 0, 1]
Output: 0
Explanation: -1 + 0 + 1 = 0
Example 3
Input: nums = [42]
Output: 42
Explanation: A single element is its own sum.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • Expected time complexity: O(n)
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run