0

Sorry if this is a duplicate. I'm looking for the best way to convert an array into a key I can use for a set in JavaScript.

const set = new Set()
const arr = [1,0,-1]
const arr2 = [1,0,-1]
set.add(arr)
set.add(arr2)

This should work because both arr and arr2 are different objects and the set recognizes their references as unique. What's the best way I can convert arr and arr2 into keys so the set recognizes them as unique?

I tried doing JSON.stringify(arr), but I had difficulty converting them back into number[].

Any help is appreciated!

Here is the full version of the code I'm trying:

function twoSum(nums: number[], excludeIndex: number, target: number)
{
    let startPointer = 0;
    let endPointer = nums.length - 1;
    while(startPointer < endPointer)
    {
        if(startPointer === excludeIndex)
        {
            startPointer++;
        }
        if(endPointer === excludeIndex)
        {
            endPointer--;
        }
        if(nums[startPointer] + nums[endPointer] === target)
        {
            return [startPointer, endPointer]
        }
        if(nums[startPointer] + nums[endPointer] > target)
        {
            endPointer--;
        }
        else{
            startPointer++;
        }
    }
    return undefined
}

function threeSum(nums: number[]): number[][] {
    if(nums.length < 3)
    {
        return []
    }
    nums.sort((a,b) => a - b);
    let index = 0;
    const uniqueTriplets = new Set()
    while(index < nums.length)
    {
        const num = nums[index]
        const twoSumResult = twoSum(nums, index, (num * -1)) 
        if(twoSumResult !== undefined)
        {
            const triplet = [num, nums[twoSumResult[0]], nums[twoSumResult[1]]]
            triplet.sort((a,b) => a - b)
            uniqueTriplets.add(JSON.stringify(triplet))
        }
        index++;
    }

    return Array.from(uniqueTriplets.keys()).map(k => JSON.parse(k))
}

On the last return statement I get an error saying: "Argument of type 'unknown' is not assignable to parameter of type 'string'." When I add the JSON.parse(k)

avenmia
  • 2,015
  • 4
  • 25
  • 49
  • what do you mean with keys? what do you want to do with them? – Nina Scholz Apr 30 '22 at 21:00
  • I'm doing a leetcode problem where I'm storing unique sets of triplets. My plan was to add them to a set then iterate over the keys. – avenmia Apr 30 '22 at 21:02
  • Do you mean they are currently treated as distinct but you want them treated as equal? Also, please define the "difficulty converting them back into number[]". It should be as easy as `JSON.parse(element)`. EDIT: Just noticed, you have tagged both [tag:javascript] and [tag:typescript]. Pick one, please. – Amadan Apr 30 '22 at 21:02
  • Weird, I did JSON.parse, but it was having issues converting types of unknown. I assumed it was because the array keys were of the format of "[-1,0,1]", but maybe it was a different issue. JSON.parse is working in the snippets in the browser though – avenmia Apr 30 '22 at 21:04
  • Could you tell us what do you mean by converting those `Arrays` into `keys`? – Zulfiqar Ali Apr 30 '22 at 21:06
  • I think I'm going to delete the question, because I think there's a different issue going on. The following works in the snippets portion of my browser, but not in my leetcode problem. const set = new Set() const arr = "[1,0,-1]" const arr2 = "[-1,-1,2]" set.add(arr) set.add(arr2) Array.from(set.keys()).map(k => JSON.parse(k)) – avenmia Apr 30 '22 at 21:08
  • `JSON.stringify` should work. If you're having trouble with (correctly typing) `JSON.parse`, please show us the code you tried – Bergi Apr 30 '22 at 21:08
  • I appreciate your comments, I added the full code along with the error I'm getting. – avenmia Apr 30 '22 at 21:12
  • I asked before, but it is all the more relevant now: pick a language. The error you are seeing is a TypeScript error. The code you provided is JavaScript. They are closely related, but different languages. If you execute the code directly in the browser, you are executing it as JavaScript that it is. If you execute it within a TypeScript environment, you get a typing error, since TypeScript is touchy about these things. `JSON.parse` requires a string, but your set was not told what kind of objects it contains, and thus `Set.keys()` returns objects of type `unknown`. – Amadan Apr 30 '22 at 21:38
  • You're correct @Amadan , I got it to work by casting Array.from(..) as string[] then called map. If you want to put that as the answser, I'll accept it. Unless this stays closed, I appreciate the help! – avenmia Apr 30 '22 at 21:49

0 Answers0