Files
JavaScript/Bit-Manipulation/UniqueElementInAnArray.js
Madhurendra Nath Tiwari f5188ddf16 added an algo which finds unique element in an array (#1359)
* added an algo which finds unique element in an array

* fixed code style

* updated changes and add some explanations

* Delete package-lock.json

* Delete package.json

* added question link if anyone want to solve

* updated changes

* added package.json

* used JSDoc comment

---------

Co-authored-by: madhuredra <madhuredra.tiwari@zemosolabs.com>
2023-09-22 14:52:11 +05:30

14 lines
406 B
JavaScript

/**
* Finds the unique element in an array where all other elements are repeated twice.
*
* @param {number[]} arr - The input array of integers.
* @returns {number} The unique element.
*
* @example
* const arr = [1, 2, 1, 2, 3];
* const uniqueElement = findUniqueElement(arr); // Returns 3
*/
const findUniqueElement = (arr) => arr.reduce((acc, val) => acc ^ val, 0)
export { findUniqueElement }