I have a scenario where I am dealing with three strings that contain comma-separated numbers.
var str1 = "1,5,3";
var str2 = "2,5,1";
var str3 = "3,2,1,5";
My objective is to find the common elements between all three strings. The expected output should be:
var result = 1,5;
If I only have two strings, I already have a logic in place which splits the string and checks for common elements:
var array = str2.split(',');
for(var item in array) {
var contains = (str1.indexOf(array[item]) > -1);
if(contains == 1) {
var result = array[item] + ',';
getele2 += result;
geteleresult = getele2.replace(/\,$/, '');
}
}
alert(geteleresult);
However, when dealing with multiple strings, I am unsure of how to apply similar logic for sorting. If you have any ideas or suggestions, please feel free to provide them. Thank you!