Last active
August 29, 2015 14:13
-
-
Save AbeEstrada/dc73b0525769383b7a87 to your computer and use it in GitHub Desktop.
Given an array of strings, detect if there are any non-repeating array elements.
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
function hasSingleElements(arr) { | |
if (arr.length > 0) { | |
var r = []; | |
for (var i = 0; i < arr.length; i++) { | |
var e = r.indexOf(arr[i]); | |
if (e > -1) { | |
r.splice(e, 1); | |
} else { | |
r.push(arr[i]); | |
} | |
}; | |
if (r.length > 0) { | |
return true; | |
} | |
} | |
return false; | |
} | |
hasSingleElements(['a', 'b', 'b', 'c', 'a', 'c']); // returns false | |
hasSingleElements([]); // returns false | |
hasSingleElements(['a']); // returns true | |
hasSingleElements(['a', 'b', 'b', 'c', 'a', 'd', 'c']); // returns true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment