I have two question
1) how can I check two shuffle string have same characters Like I have
var str1 = \"ansar@#//1\";
var str2 = \"@#//sanra1\";
This will return a true or false, if character in both string having the same character, and i think this is the most efficent way to do that.
a)
function hasSameCharacter(str1, str2) {
let a = Array.prototype.every.call(str1, (char) => str2.indexOf(char) > -1, this);
if (a) return Array.prototype.every.call(str2, (char2) => str1.indexOf(char2) > -1, this);
else return false;
}
console.log(hasSameCharacter(str1, str2));
b)
function hasSameCharacter(str1, str2) {
for (let i = 0; i < str1.length; i++) {
if (str2.indexOf(str1[i]) <= -1) return false;
}
for (let i = 0; i < str2.length; i++) {
if (str1.indexOf(str2[i]) <= -1) return false;
}
return true;
}
console.log(hasSameCharacter(str1, str2));
Hope this helps, happy coding :)
So I can't comment, because I don't have the ratings, but I tried Yuriy Yakym's solution (the second one, and a massive thank you Yuriy because I'd have struggled without that to work with in the beginning) and whilst it works for this example, if you add another 'a' say to str1, it won't show you that str2 doesn't actually have 3 'a's, because it's always going to reference the first appearance of the said character. In essence, you need to delete the found characters to ensure you are accounting for duplicates, or so it seems.
I developed the below from Yuriy's code to get it working for me. Please test it yourself. Happy to be disagreed with, but it seems, it will log out everything (including duplicates) that appears in str1 but doesn't appear inside str2:
const sorter = (str1, str2) => {
const arr = str1.split("");
const newArray = arr.filter((c) => {
if (str2.indexOf(c) === -1) {
return str2.indexOf(c) === -1
} else {
str2 = str2.replace(c, '');
}
})
return newArray.join("");
};
console.log(sorter(str1, str2));
Consider these implementations:
1)
var str1 = "ansar@#//1";
var str2 = "@#//sanra1";
function first(str1, str2) {
return Array.prototype.every.call(str1, function(c) {
return str2.indexOf(c) > -1;
}, this);
}
console.log(first(str1, str2));
var str1 = "ansar@#//123";
var str2 = "@#//sanra";
function second() {
return Array.prototype.filter.call(str1, function(c) {
return str2.indexOf(c) === -1;
}, this).join('');
}
console.log(second(str1, str2));
Update for checking if strings contain same characters you can use ES6 Set:
const checkIfStringsContainSameCharacters = (string1, string2) => {
return new Set(string1).size === new Set(string1 + string2).size;
}
using a while loop seems a reasonable solution:
var str1 = "ansar@#//1";
var str2 = "@#//sanra12";
s1 = str1.split('');
s2 = str2.split('');
var i = s1.length + 1;
while (i--) {
if (s2.indexOf(s1[i]) >= 0)
s2.splice(s2.indexOf(s1[i]), 1);
}
console.log(s2)
the resulting array represents the letters of str2
not matched in str1
this will return empty string if character set in both strings is the same.
function findDiff (str1, str2)
{
var diff = '';
if (str1.length > str2.length)
{
var search = str1;
var compare = str2;
}
else
{
var search = str2;
var compare = str1;
}
for (var i = 0; i < search.length; i++)
{
var symbol = search[i];
if (compare.indexOf(symbol) === -1)
{
diff += symbol;
}
}
return(diff);
}
findDiff("ansar@#//123", "@#//sanra");
https://jsfiddle.net/tadaspaplauskas/pn7jnj8e/
This code may help you to get output as per your wish.
var str1 = "ansar@#//g123";
var str2 = "@#//sanraD";
function strDiff(s1, s2){
var t,inter,a1,a2;
a1 = s1.split('');
a2 = s2.split('');
t = a2, a2 = a1, a1 = t;
inter = a1.filter(function (e) {
if (a2.indexOf(e) !== -1) return true;
});
for (i=0, len=inter.length; i<len; i++) {
for(var j = a1.length-1; j>=0;j--)
if (a1[j] === inter[i]) a1.splice(j, 1);
for(var k = a2.length-1; k>=0; k--)
if (a2[k] === inter[i]) a2.splice(k, 1);
}
if((a1.join('')+a2.join('')).length > 0)
return(a1.join('')+a2.join(''));
else
return "True";
}
var result = strDiff(str2,str1);
alert(result);