chore(Javascript): single occurring element among duplicates (#969)
parent
04d42af7c0
commit
0816bfcddd
|
@ -3,6 +3,7 @@
|
||||||
## Arrays
|
## Arrays
|
||||||
|
|
||||||
- [Counting Inversions](src/arrays/counting-inversions.js)
|
- [Counting Inversions](src/arrays/counting-inversions.js)
|
||||||
|
- [Single Occurring Element](src/arrays/single-occurring-element.js)
|
||||||
|
|
||||||
## Linked Lists
|
## Linked Lists
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
// Problem: Given an array of integers,
|
||||||
|
// every element appears twice except for one. Find that single one.
|
||||||
|
// Space Complexity: O(1)
|
||||||
|
// Time Complexity: O(n)
|
||||||
|
|
||||||
|
function singleOccurringElement(arr) {
|
||||||
|
let result = 0;
|
||||||
|
for (const el of arr) {
|
||||||
|
result ^= el;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
const arr = [2, 5, 7, 3, 1, 8, 8, 9, 4, 2, 7, 1, 4, 9, 5];
|
||||||
|
console.log(singleOccurringElement(arr));
|
||||||
|
|
||||||
|
// Input: [2, 5, 7, 3, 1, 8, 8, 9, 4, 2, 7, 1, 4, 9, 5]
|
||||||
|
// Output: 3
|
|
@ -1,5 +1,6 @@
|
||||||
// Arrays
|
// Arrays
|
||||||
require('./arrays/counting-inversions');
|
require('./arrays/counting-inversions');
|
||||||
|
require('./arrays/single-occurring-element');
|
||||||
|
|
||||||
// Linked Lists
|
// Linked Lists
|
||||||
require('./linked-lists/singly');
|
require('./linked-lists/singly');
|
||||||
|
|
Loading…
Reference in New Issue