Single Number

easy

By - Aman Pareek

Last Updated - 04/09/2024

Problem Statement

You are given a non-empty array of integers, nums. In this array, every element appears exactly twice except for one unique element which appears only once. Your task is to identify and return this unique element.

Example 1

Input: array = [2,2,1]

Output: 1

Example 2

Input: array = [4,1,2,1,2]

Output: 4

Constraints

  • The array nums will always contain at least one element and up to 30,000 elements.

  • Each integer in nums will be in the range of −30,000-30,000−30,000 to 30,00030,00030,000.

  • There will always be exactly one unique element in the array that appears only once.

Solution 1: Using XOR Operator

function singleNumberXOR(nums) {
    let unique = 0;
    for (const num of nums) {
        unique ^= num;
    }
    return unique;
} 

const array1 = [2,2,1];
singleNumberXOR(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberXOR(array2);  //output: 4 

Solution 2: Using a Set

function singleNumberSet(nums) {
    const seen = new Set();
    for (const num of nums) {
        if (seen.has(num)) {
            seen.delete(num);
        } else {
            seen.add(num);
        }
    }
    return [...seen][0];
} 

const array1 = [2,2,1];
singleNumberSet(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberSet(array2);  //output: 4 

Solution 3: Using a Hash Map

function singleNumberHashMap(nums) {
    const counts = new Map();
    for (const num of nums) {
        counts.set(num, (counts.get(num) || 0) + 1);
    }
    for (const [num, count] of counts) {
        if (count === 1) {
            return num;
        }
    }
} 

const array1 = [2,2,1];
singleNumberHashMap(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberHashMap(array2);  //output: 4 

Solution 4: Using Sorting

function singleNumberSort(nums) {
    nums.sort((a, b) => a - b);
    for (let i = 0; i < nums.length - 1; i += 2) {
        if (nums[i] !== nums[i + 1]) {
            return nums[i];
        }
    }
    return nums[nums.length - 1]; // The last element is the unique one if not found in the loop
} 

const array1 = [2,2,1];
singleNumberSort(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberSort(array2);  //output: 4 

Solution 5: Using Array.prototype.reduce()

function singleNumberReduce(nums) {
    return nums.reduce((unique, num) => unique ^ num, 0);
} 

const array1 = [2,2,1];
singleNumberReduce(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberReduce(array2);  //output: 4 

Solution 6: Using Map with Count

function singleNumberMapCount(nums) {
    const countMap = new Map();
    nums.forEach(num => {
        countMap.set(num, (countMap.get(num) || 0) + 1);
    });
    for (const [num, count] of countMap.entries()) {
        if (count === 1) {
            return num;
        }
    }
} 

const array1 = [2,2,1];
singleNumberMapCount(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberMapCount(array2);  //output: 4 

Solution 7: Using Array.prototype.filter()

function singleNumberFilter(nums) {
    return nums.filter(num => nums.indexOf(num) === nums.lastIndexOf(num))[0];
} 

const array1 = [2,2,1];
singleNumberFilter(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberFilter(array2);  //output: 4 

Solution 8: Using Array.prototype.reduce() with Object

function singleNumberReduceObject(nums) {
    const countObj = nums.reduce((obj, num) => {
        obj[num] = (obj[num] || 0) + 1;
        return obj;
    }, {});
    return +Object.keys(countObj).find(key => countObj[key] === 1);
} 

const array1 = [2,2,1];
singleNumberReduceObject(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberReduceObject(array2);  //output: 4 

Solution 9: Using a Custom Class with a Set

function singleNumberCustomClass(nums) {
    const finder = new SingleNumberFinder();
    nums.forEach(num => finder.add(num));
    return finder.getUnique();
}


class SingleNumberFinder {
    constructor() {
        this.set = new Set();
    }

    add(num) {
        if (this.set.has(num)) {
            this.set.delete(num);
        } else {
            this.set.add(num);
        }
    }

    getUnique() {
        return [...this.set][0];
    }
} 

const array1 = [2,2,1];
singleNumberCustomClass(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberCustomClass(array2);  //output: 4 

Solution 10: Using Array.prototype.forEach() with Object

function singleNumberForEach(nums) {
    const countMap = {};
    nums.forEach(num => {
        countMap[num] = (countMap[num] || 0) + 1;
    });
    return +Object.keys(countMap).find(key => countMap[key] === 1);
} 

const array1 = [2,2,1];
singleNumberForEach(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberForEach(array2);  //output: 4 

Solution 11: Using Array.prototype.find() with a Hash Set

function singleNumberFindSet(nums) {
    const seen = new Set();
    const duplicates = new Set();
    
    nums.forEach(num => {
        if (seen.has(num)) {
            duplicates.add(num);
        }
        seen.add(num);
    });
    
    return [...seen].find(num => !duplicates.has(num));
} 

const array1 = [2,2,1];
singleNumberFindSet(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberFindSet(array2);  //output: 4 

Solution 12: Using Array.prototype.reduce() with a Map

function singleNumberReduceMap(nums) {
    const countMap = nums.reduce((map, num) => {
        map.set(num, (map.get(num) || 0) + 1);
        return map;
    }, new Map());
    
    for (const [num, count] of countMap) {
        if (count === 1) {
            return num;
        }
    }
} 

const array1 = [2,2,1];
singleNumberReduceMap(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberReduceMap(array2);  //output: 4 

Solution 13: Using a Frequency Array

function singleNumberFrequencyArray(nums) {
    // Create a map to store the frequency of each number
    const frequencyMap = new Map();
    
    // Count occurrences of each number
    nums.forEach(num => {
        frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
    });
    
    // Find the number that appears exactly once
    for (const [num, count] of frequencyMap) {
        if (count === 1) {
            return num;
        }
    }
} 

const array1 = [2,2,1];
singleNumberFrequencyArray(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberFrequencyArray(array2);  //output: 4 

Solution 14: Using Array.prototype.indexOf()

function singleNumberIndexOf(nums) {
    return nums.find(num => nums.indexOf(num) === nums.lastIndexOf(num));
} 

const array1 = [2,2,1];
singleNumberIndexOf(array1);  //output: 1 

const array2 = [4,1,2,1,2];
singleNumberIndexOf(array2);  //output: 4 

Resources

Popular Solutions