Given a list of integers, return a new list containing the difference between each pair of neighbouring values. The value at position i of the result is nums[i + 1] - nums[i], so the result always has exactly one fewer element than the input.
If the input list is null or has fewer than 2 elements there are no pairs to compare, so return an empty list.
Examples:
Input: nums = [3, 7, 2, 9]
Output: [4, -5, 7]
Explanation: 7 - 3 = 4, then 2 - 7 = -5, then 9 - 2 = 7
Input: nums = [5, 5, 5]
Output: [0, 0]
Explanation: Neighbouring values are equal, so every difference is 0
Input: nums = [-2, 3]
Output: [5]
Explanation: 3 - (-2) = 5
Input: nums = [10]
Output: []
Explanation: A single element has no neighbour to compare against
Input: nums = []
Output: []
Explanation: An empty list produces an empty result
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay November 2026 (Nov 1–15)Sponsored by FLXBL
Contest runs November 1 - 15. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.