chore(Javascript): single occurring element among duplicates (#969)

pull/995/merge
Pranav Rustagi 2022-10-12 06:55:25 +05:30 committed by GitHub
parent 04d42af7c0
commit 0816bfcddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -3,6 +3,7 @@
## Arrays
- [Counting Inversions](src/arrays/counting-inversions.js)
- [Single Occurring Element](src/arrays/single-occurring-element.js)
## Linked Lists

View File

@ -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

View File

@ -1,5 +1,6 @@
// Arrays
require('./arrays/counting-inversions');
require('./arrays/single-occurring-element');
// Linked Lists
require('./linked-lists/singly');