I have an array of 800 sentences. I want to remove all duplicates (sentences that have the same exact words, but in different order) from the array. So for example \"this is
This is a very simple implementation that takes advantage of some jQuery.
Check the demo here ->
And the source:
var arr = ["This is a sentence", "Is this a sentence", "potatoes"];
var newArr = [];
var sortedArr = [];
$.each(arr, function(i) {
var temp = this.toLowerCase().split(" ").sort(function(a,b) {
return a > b;
}).join(' ');
if ($.inArray(temp, sortedArr) == -1) {
sortedArr.push(temp);
newArr.push(arr[i]);
}
});
//output
$.each(newArr, function() {
document.write(this + '
');
});
It uses three arrays: a source, a collection of sorted sentences to match against, and the output array. Matching is performed by splitting a sentence by spaces, converting to lowercase, and sorting the words alphabetically, then rebuilding the sentence string. If that particular combo has been seen before, it is not added to the results. If it has not, it is added.
The loop at the end simply outputs the resultant array.