Sum of Elements

Easy~10 min

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

For example, given [1, 2, 3, 4], the sum is 1 + 2 + 3 + 4 = 10.

If the array is empty, return 0.

Examples

Example 1
Input: nums = [1, 2, 3, 4]
Output: 10
Explanation: 1 + 2 + 3 + 4 = 10
Example 2
Input: nums = [-1, 1, -2, 2]
Output: 0
Explanation: Positive and negative values cancel out.
Example 3
Input: nums = [100]
Output: 100
Explanation: A single element is its own sum.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The answer fits in a 64-bit signed integer.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run