Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Last active January 12, 2016 22:35
Show Gist options
  • Save brianswisher/7ed8f8d9867da086a901 to your computer and use it in GitHub Desktop.
Save brianswisher/7ed8f8d9867da086a901 to your computer and use it in GitHub Desktop.
Get Bachelor - return odd occurrence in array
(function(input){
function solution(A){
var input = (function(bachelor){
if (typeof A !== "object") throw new Error("A is not an object.");
if (typeof A.split === "function") throw new Error("A is not an array.");
if (!(A.length % 2)) throw new Error("A.length even.");
return {
squashCouples: function(lookup){
lookup = {};
A.map(function(val){
val = val.toString();
if (!lookup[val]){
lookup[val] = true;
} else {
delete(lookup[val]);
}
});
A = lookup;
return this;
},
getBachelor: function(){
return parseInt(Object.keys(A).pop());
}
};
})();
return input
.squashCouples()
.getBachelor();
}
return solution(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment