Why is this not splicing?

I’m doing a hacker rank challenge and I can’t figure out why my solution won’t work.

function getSecondLargest(nums) {
    var big = Math.max.apply(Math, nums);
    
    for (var i = 0; i < nums.length; i++) {
        
        if (nums[i] == big) { 
            nums.splice(nums[i], 1);
            console.log(nums);
        }
        

    }

    for (var i = 0; i < nums.length; i++) {
        var secondBig = Math.max.apply(Math, nums);
        if (nums[i] == secondBig) {
            return nums[i];
        }


    }

}

more specifically I cant seem to get splice to remove the largest int in the array

A couple of tips. The first one is that you can do the Math.max in a way that I like more since I’m a noob. The way you do this is:

Math.max(…nums);

I like that much more.

The issue that’s causing your code not to work is in your splice.

Hint: As it is right now, let’s say nums = [10, 20, 30, 40]. When big (40) == nums[i], you’re asking to splice at index nums[i] which would be index 40

Yes, I see now. I’ve tried replacing it with nums.splice(indexOf(big), 1); But that doesnt seem to work either.
or maybe?
nums.splice(nums[indexOf(big)], 1);

I’ve been stuck on this longer then I’d like to admit…

nums.splice(indexOf(big),1) is a very good idea, actually. You don’t even need the loop or anything. If you just were to do this:

function getSecondLargest(nums) {
var big = Math.max(…nums);
nums.splice(nums.indexOf(big),1);
}

console.log(getSecondLargest([1,2,3,4,5])); // edit: should return [1,2,3,4]

So I’m not sure why it didn’t work for you but that should work

Try exactly what you have but inside the first for loop if statement change your splice to nums.splice(i, 1).

Splice wants the index not the element of the array.

I think I’d need a loop because the max number appears in the array twice.

That worked, thank you.

Thank you, that cleared somethings up.

I solved it for all test cases. I feel I made this much more complex then it needed to be but hey it worked.


function getSecondLargest(nums) {
    var big = Math.max.apply(Math, nums);

    for (var i = 0; i < nums.length; i++) {

        if (nums[i] == big) {
          var removed =  nums.splice(i, 1);

            for (var j = 0; j < nums.length; j++) { 

                if (nums[j] == Number(removed)) { 

                    getSecondLargest(nums);
                }
            }
            
        }

    }

    for (var i = 0; i < nums.length; i++) {
        var secondBig = Math.max.apply(Math, nums);
        if (nums[i] == secondBig) {
            return nums[i];
        }

    }

}
1 Like

haha good job! That is complicated! Since you finished yours, I’ll show you what I came up with!

function getSecondLargest(nums) {
    let biggest = Math.max(...nums);
    let newArr = [];
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== biggest) {
            newArr.push(nums[i]);
        }
    }
    return Math.max(...newArr);
}

haha clever, didn’t think of that.:rofl:
I suck at algorithms, that’s why I’ve been doing hackerrank problems every day

Using some ES6 features:

function getSecondLargest(nums) {
    return [...new Set(nums.sort((a, b) => b - a))][1];
}
3 Likes

Definitely gonna look up what that even is. Just goes to show that there’s always a more elegant solution

Set is data structure like array (but not really array) where every element is unique.
If you pass array as an argument when creating a Set, it’ll remove all duplicates. Then using spread (...) you can convert Set to an array.

This is the modern way to remove duplicates from an array:

const arrayNoDuplicates = [...new Set(arrayWithDuplicates)]