I\'m trying to find the items that appear only one time in a Javascript array. In the following array:
[\'txfa2\',\'txfa9\',\'txfa2\',\'txfa1\',\'txfa3\',\'t
a simple approach will be making advantage of Javascript's builtin object. Let an object act as a counter for each item in the collection, then iterate over it to check which item has a counter of 1.
it's quite fast;
var items = ['txfa2', 'txfa9', 'txfa2', 'txfa1', 'txfa3', 'txfa4', 'txfa8', 'txfa9', 'txfa2', 'txfa8'],
result = [],
i,
k,
container = {};
for (i = 0; i < items.length; ++i) {
if (items[i] in container) {
container[items[i]]++;
} else {
container[items[i]] = 1;
}
}
for (k in container) {
if (container[k] == 1) {
result.push(k);
}
}
console.log(result)