distinct pairs in js
by SriSri M
HTML
<div class="distinct">
<label for="" id="txt"></label>
</div>
JavaScript
var data = ["u1,u2","u2,u1","u3,u4","u4,u3"];
var x,y,z,bool;
var tempArr = [];
var distinct = [];
var splitArr;
//1. Split Array
for(x=0;x < data.length; x++) {
splitArr = data[x].split(',');
tempArr.push(splitArr);
}
//2. Sort Array
for(y=0; y < tempArr.length; y++) {
tempArr[y].sort(function(a,b){
return a > b;
});
}
//3. Join Array
for(z=0; z < tempArr.length; z++) {
tempArr[z] = tempArr[z].join(',');
}
//4. Find Unique Pairs
var p,q;
for(p=0; p < tempArr.length; p++) {
bool = true;
for(q=0; q < tempArr.length; q++) {
if(distinct[q] == tempArr[p]) {
bool = false;
}
}
//5. Push Distinct pairs to Unique arr;
if(bool) {
distinct.push(tempArr[p]);
}
}
console.log(distinct);