Set.has unable to accurately identify if it contains an array or object

Viewed 13

I was working on this leetcode problem where I am supposed to identify if the linked list is a cycle or has an end.

I stumbled across a solution which made perfect sense however I don't understand how set.has is able to correctly identify whether the node has been stored before

let set = new Set()
var hasCycle = function(head) {
    set = new Set();
    return loopThrough(head,0);
};

function loopThrough(node){
    if(node === null){
        return false;
    }
    else if(set.has(node)){
        return true;
    }
    else{
         set.add(node)
    }
    return loopThrough(node.next)
}

I tested out set.has on a separate terminal where the set contains either an object/array but it isn't able to accurately identify if the set already contains the object. enter image description here

Can someone kindly tell me why set.has works for the leet code solution but not when I test it in a separate IDE?

0 Answers
Related