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
function singleNumberXOR(nums) {
let unique = 0;
for (const num of nums) {
unique ^= num;
}
return unique;
}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];
}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;
}
}
}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
}function singleNumberReduce(nums) {
return nums.reduce((unique, num) => unique ^ num, 0);
}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;
}
}
}function singleNumberFilter(nums) {
return nums.filter(num => nums.indexOf(num) === nums.lastIndexOf(num))[0];
}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);
}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];
}
}function singleNumberForEach(nums) {
const countMap = {};
nums.forEach(num => {
countMap[num] = (countMap[num] || 0) + 1;
});
return +Object.keys(countMap).find(key => countMap[key] === 1);
}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));
}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;
}
}
}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;
}
}
}function singleNumberIndexOf(nums) {
return nums.find(num => nums.indexOf(num) === nums.lastIndexOf(num));
}