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.
Input: array = [2,2,1]
Output: 1
Input: array = [4,1,2,1,2]
Output: 4
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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