Skip to content

Instantly share code, notes, and snippets.

@AbeEstrada
Last active August 29, 2015 14:13
Show Gist options
  • Save AbeEstrada/dc73b0525769383b7a87 to your computer and use it in GitHub Desktop.
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.
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