136. Single Number

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function singleNumber($nums) {
        $collect = [];
        $pointers = [];
        foreach($nums as $index => $num) {
            $collect[$num] ??= 0;
            ++$collect[$num];
            if ($collect[$num] === 1){
                $pointers[$num] = $index;
            } else {
                unset($pointers[$num]);
            }
        }

        $answer = array_pop($pointers);
        return $nums[$answer];
    }
}

https://leetcode.com/problems/single-number/solutions/3596185/found-all-single-numbers

Last updated