I am new at Programing and learning Javascript by doing some exercises from leetcode.com
.
I wanted to write a code to remove duplicates in a sorted array.
When I use "console.log" at the end of the function to show the final result, I get the expected result. However when I use return (with the same variable) I get a wrong result. Can anybody please tell me, where I went wrong?
Hier is my code:
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function (nums) {
var newNums = []
if (nums.length == 0) {
newNums = []
}
for (var i = 0; i < nums.length; i++) {
var curr = nums[i]
var next = nums[i + 1]
if (curr != next) {
newNums.push(curr)
}
}
console.log(newNums)
return newNums
};
And here is a picture with the code and the results. the green arrow shows the output of (console.log), and the red one shows the output of (return).
Thank you in advance!