easy Array

Sort Array

Write a function sortArray that takes an array of integers and returns a new array with the elements sorted in ascending order.

Function Signature

function sortArray(nums);

Example 1:

Input: nums = [34, 7, 23, 32, 5, 62]
Output: [5, 7, 23, 32, 34, 62]
Explanation: After sorting the numbers from the smallest to the largest, the output is [5, 7, 23, 32, 34, 62].

Example 2:

Input: nums = [-1, -3, -2, 0, 2, 1]
Output: [-3, -2, -1, 0, 1, 2]
Explanation: Negative numbers are sorted first followed by zero and positive numbers, resulting in [-3, -2, -1, 0, 1, 2].

Example 3:

Input: nums = [10]
Output: [10]
Explanation: Since there is only one element in the array, the sorted array remains [10].

Solve this problem

Start your 7-day free trial to solve this problem in our code editor with instant feedback.

Start 7-Day Free Trial to Solve This Problem

Related Problems

Find Max

easy

Write a function `findMax` that takes an array of integers and returns the maximum value. ## Function Signature ```function findMax(nums);``` ### Example 1: > **Input:** `nums = [1, 5, 3, 9, 2]`\ > **Output:** `9`\ > **Explanation:** The maximum value in the array is `9`. ### Example 2: > **Input:** `nums = [-1, -5, -3, -9, -2]`\ > **Output:** `-1`\ > **Explanation:** The maximum value in the array is `-1`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `10`\ > **Explanation:** The maximum value in the array is `10`.

Array

Sum Array

easy

Write a function `sumArray` that takes an array of integers and returns the sum of all elements. ## Function Signature ```function sumArray(nums);``` ### Example 1: > **Input:** `nums = [1, 2, 3, 4, 5]`\ > **Output:** `15`\ > **Explanation:** The sum of the numbers is `1 + 2 + 3 + 4 + 5 = 15`. ### Example 2: > **Input:** `nums = [-1, -2, -3, -4, -5]`\ > **Output:** `-15`\ > **Explanation:** The sum of the numbers is `-1 + -2 + -3 + -4 + -5 = -15`. ### Example 3: > **Input:** `nums = [0]`\ > **Output:** `0`\ > **Explanation:** The sum of the numbers is `0`.

Array

Find Min

easy

Write a function `findMin` that takes an array of integers and returns the minimum value. ## Function Signature ```function findMin(nums);``` ### Example 1: > **Input:** `nums = [1, 5, 3, 9, 2]`\ > **Output:** `1`\ > **Explanation:** The minimum value in the array is `1`. ### Example 2: > **Input:** `nums = [-1, -5, -3, -9, -2]`\ > **Output:** `-9`\ > **Explanation:** The minimum value in the array is `-9`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `10`\ > **Explanation:** The minimum value in the array is `10`.

Array