5

I was going about solving some algos on binarysearch.io and came across this problem:

enter image description here

I solved it using JS as follows:

class Solution {
    solve(nums) {
        const Hash = {};
        const Cache = {};
        
        for (let val of nums) {
            if (!Hash[val]) {
                Hash[val] = 0;
            }
            Hash[val]++; //implicit else
        }

        const result = Object.values(Hash).reduce((accum, curr) => {
            if (accum && Cache[curr] !== 'unique') {
                Cache[curr] = 'unique';
                return true
            }
            return false;
        }, true);
        
        return result;
    }
}

However, when I tried to solve it with TypeScript, which I'm fairly new to, and I had compilation errors likely caused by the .reduce() method:

class Solution {
    solve(nums: Array<number>): boolean {
        const Hash = {};
        const Cache = {};
        
        for (let val of nums) {
            if (!Hash[val]) {
                Hash[val] = 0;
            }
            Hash[val]++; //implicit else
        }
        
        const val_arr = Object.values(Hash);

        return val_arr.reduce((accum, curr) => {
            if (accum && Cache[curr] !== 'unique') {
                Cache[curr] = 'unique';
                return true;
            }
            return false;
        }, true);
    }
}

I'm not certain how to provide the .reduce() method a return type for the seeded initial value. I've tried changing the initial value to have a boolean type, but I still have a compilation error:

return val_arr.reduce((accum, curr) => {
            if (accum && Cache[curr] !== 'unique') {
                Cache[curr] = 'unique';
                return true;
            }
            return false;
        }, true as boolean);

I tried searching if anyone else has this problem, but most posts I've found have objects as the initial value, so I couldn't quite figure it out. It almost appears like the easiest solution is to solve this is to refactor to solve this without using .reduce(), but I simply want to know how to create a TS 'equivalent' to the aforementioned JS solution.

1 Answer 1

4

You need to

(1) Type the Hash object properly so that the type of its keys and values can be recognized by TS

(2) Type the Cache object properly so that the type of its keys and values can be recognized by TS

(3) Type the accumulator as a boolean, rather than true, so that the callback inside can return either true or false. (This can be done by passing <boolean> as the type argument to .reduce - using generics is a bit preferable to as)

class Solution {
    solve(nums: Array<number>): boolean {
        const Hash: {[key: number]: number} = {};
        const Cache: {[key: number]: string} = {};
        
        for (let val of nums) {
            if (!Hash[val]) {
                Hash[val] = 0;
            }
            Hash[val]++; //implicit else
        }
        
        const val_arr = Object.values(Hash);

        return val_arr.reduce<boolean>((accum, curr) => {
            if (accum && Cache[curr] !== 'unique') {
                Cache[curr] = 'unique';
                return true;
            }
            return false;
        }, true);
    }
}

Another thing to consider - JavaScript is not Java. If you have a class that just contains a single method, with no references to instance variables, it would make much more sense structurally to define a plain function instead, eg:

function solve(nums: Array<number>) {
  // function implementation
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I greatly appreciate your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.