Lonely Integer

  • + 0 comments
    function lonelyinteger(a: number[]): number {
        if (a.length === 1) return a[0];
        
        const arrMap = new Map<number, number>();
        
        a.forEach((n:number) => (arrMap.has(n)) ? arrMap.set(n, (arrMap.get(n) + 1)) : arrMap.set(n, 1));
        const iterator = arrMap.entries();
        
        for (let i = 0; i < arrMap.size; i++){
            const results = iterator.next().value;
            if (results[1] === 1) return results[0]
        }
    }