Beautiful Pairs

  • + 0 comments

    Here is my typescript solution:

    function beautifulPairs(A: number[], B: number[]): number {
        let counter: number = 0;
        
        /*
        * Looping through the A array and finding if there are any matched in the,
        * if there are then we count up and remove the element from the B array to
        * prevent duplicates being counted again
        */
        A.forEach((num) => {
            if (B.includes(num)) {
                counter ++;
                const indexToRemove: number = B.indexOf(num);
                B.splice(indexToRemove, 1)
            }
        })
        
        return counter === A.length ? counter -1 : counter +1;
    
    }