Last active
November 18, 2016 17:52
-
-
Save rohan-paul/bac5818a64b1c3aafd2554c6601e5ad4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First generate a long enough array sequence to test the code with. | |
var generateArray = Array.from(new Array(100000),(val,index)=>index); | |
var startMyCode = Date.now(); | |
function findOddMyCode(A) { | |
var len = A.length; | |
var A_sort = A.slice().sort(); | |
var count = {}; | |
A_sort.forEach(function(i) { | |
count[i] = (count[i] || 0) + 1; | |
}); | |
for (var key in count) { | |
if (count.hasOwnProperty(key)) { | |
// var value = count[key]; | |
if (count[key] % 2 !== 0) { | |
return Number(key); | |
} | |
} | |
} | |
} | |
findOddMyCode(generateArray); | |
var endMyCode = Date.now(); | |
//End of my solution to the problem. | |
var startFastCode = Date.now(); | |
const findOddFastCode = (xs) => (xs).reduce((a, b) => a ^ b); | |
findOddFastCode(generateArray); | |
var endFastCode = Date.now(); | |
// Test the execution time difference | |
console.log((endMyCode - startMyCode) - (endFastCode - startFastCode)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment