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
Here's an example using ES5's functional methods, based on using an object to count the number of times each value occurs:
function uniq(a) {
// create a map from value -> count(value)
var counts = a.reduce(function(o, k) {
o[k] = o[k] ? o[k] + 1 : 1;
return o;
}, {});
// find those that only appeared once
return Object.keys(counts).filter(function(k) {
return (counts[k] === 1);
});
}
console.log(
uniq(['txfa2', 'txfa9', 'txfa2', 'txfa1', 'txfa3', 'txfa4', 'txfa8', 'txfa9', 'txfa2', 'txfa8'])
)
Working demo at http://jsfiddle.net/alnitak/shyce/