-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathSingleElement.js
More file actions
25 lines (23 loc) · 751 Bytes
/
SingleElement.js
File metadata and controls
25 lines (23 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/** https://leetcode.com/problems/single-number/description/
* This function will accept an array and
* Find an element which has only one frequency among other duplicate elements
* The solution is based on using two loops, if the array is large, sorting or hashing is necessary, we can use XOR operator or sum method too
* @param {Array} arr array with elements of integer type
* @returns {Number} with single element
*/
function SingleElement(arr) {
let n = arr.length
for (let i = 0; i < n; i++) {
let count = 0
// previous elements are already checked
for (let j = 0; j < n; j++) {
if (arr[j] === arr[i]) {
count++
}
}
if (count === 1) {
return arr[i]
}
}
}
export { SingleElement }