3

I have two strings:

var a = 'ABCD';
var b = 'DEFG';

I need to compare these variables to check if there is not a common CHARACTER in the two strings.

So for this case return false (or do something...) because D is a common character in them.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Fawad Mueed
  • 169
  • 1
  • 2
  • 9
  • 2
    Did you try something on your part.. If so share the same and the community can help in solving the issues. Refer the link https://stackoverflow.com/help/mcve would help in asking questions that would receive more attention – Muralidharan.rade Dec 15 '17 at 19:36
  • Possible duplicate of [JavaScript/Lodash intersection of two strings](https://stackoverflow.com/questions/41708035/javascript-lodash-intersection-of-two-strings) – Rich Churcher Dec 15 '17 at 19:39
  • You should provide something you tried here. `['ABCD', 'DEFG'].join('').match(/[a-b]{2,}/i).length > 0` would be one of many solutions. – KooiInc Dec 15 '17 at 19:45
  • Set those strings as arrays and then use the solution here: [link](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – iVoidWarranties Dec 15 '17 at 20:15

6 Answers6

4

You could merge the two strings then sort it then loop through it and if you find a match you could then exit out the loop.

I found this suggestion on a different stack overflow conversation:

var str="paraven4sr";
var hasDuplicates = (/([a-zA-Z]).*?\1/).test(str)    

So if you merge the strings together, you can do the above to use a regexp, instead of looping.

samoan
  • 98
  • 6
  • 1
    This assumes the original strings don't contain any duplicates. – Barmar Dec 15 '17 at 20:50
  • What does? The express or the post? The expression above does look for dups and I mentioned merging the two strings first. – samoan Dec 16 '17 at 00:04
  • 1
    If one of the original strings has duplicate letters, then when you merge them you'll have duplicates even if there was nothing in common between the two strings. E.g. if you start with `"ABA"` and `"C"`, they'll merge into `"ABAC"`, and you'll return `true` because of the duplicated `A`. – Barmar Dec 16 '17 at 00:17
  • Oh I miss read the issue, I thought it was just to identify duplicates but I see it is to verify duplicates only if they are in separate strings. – samoan Dec 16 '17 at 05:22
  • Another way to do it, is to sort both strings separately, check the lengths ang go through a if they are the same length, go through the shorter one if they are different. Starting with the first string, compare the first letter of the first string to the first of the second string, if the letter is < the first letter of the second, increment the first string, if it is great increment the second string, and if equal then break out the loops and return true has duplicates. If you get to the end of either string then return false. I know this is years later but I just stumbled on this question – samoan Oct 26 '21 at 22:55
4

Thank you every one. I tried your solutions, and finally got this :

  • Merging my two strings into one
  • to Lower Case,
  • Sort,
  • and Join,
  • using Regex Match if the Final Concatenated string contains any repetitions,
  • Return 0 if no Repeat occur or count of repeats.

    var a; var b; 
    var concatStr=a+b;
    checkReptCharc=checkRepeatChrcInString(concatStr);
    function checkRepeatChrcInString(str){
    console.log('Concatenated String rec:' +  str);
    try{ return 
    str.toLowerCase().split("").sort().join("").match(/(.)\1+/g).length; }
    catch(e){ return 0; } 
     }
    
Fawad Mueed
  • 169
  • 1
  • 2
  • 9
2

I was also searching for solution to this problem, but came up with this:

a.split('').filter(a_ => b.includes(a_)).length === 0

Split a into array of chars, then use filter to check whether each char in a occurs in b. This will return new array with all the matching letters. If length is zero, no matching chars.

add toUpperCase() to a & b if necessary

Tuomas H
  • 21
  • 2
0

So if it only duplicate strings in separate string arrays using .split(''), then I would sort the two string separately, and then do a binary search, start with the array of the shortest length, if the same length the just use the first one, and go character by character and search to see if it is in the other string.

samoan
  • 98
  • 6
0

This is obviously too late to matter to the original poster, but anyone else who finds this answer might find this useful.

var a = 'ABCD';
var b = 'DEFG';

function doesNotHaveCommonLetter(string1, string2) {
    // split string2 into an array
    let arr2 = string2.split("");
    // Split string1 into an array and loop through it for each letter.
    // .every loops through an array and if any of the callbacks return a falsy value,
    //     the whole statement will end early and return false too.
    return string1.split("").every((letter) => {
        // If the second array contains the current letter, return false
        if (arr2.includes(letter)) return false;
        else {
            // If we don't return true, the function will return undefined, which is falsy
            return true;
        } 
    })
}

doesNotHaveCommonLetter(a,b) // Returns false
doesNotHaveCommonLetter("abc", "xyz") // Returns true
AlpacaFur
  • 194
  • 2
  • 12
0
const _str1 = 'ABCD';
const _str2 = 'DEFG';

function sameLetters(str1, str2) {
  if(str1.length !== str2.length) return false;
  
  const obj1 = {}
  const obj2 = {}
 
 for(const letter of str1) {
    obj1[letter] = (obj1[letter] || 1) + 1
 } 
 for(const letter of str2) {
    obj2[letter] = (obj2[letter] || 1) + 1
 }
  
 for(const key in obj1) {
    if(!obj2.hasOwnProperty(key)) return false
    if(obj1[key] !== obj2[key]) return false
  }
  return true
}

sameLetters(_str1, _str2)
Armen Nersisyan
  • 313
  • 2
  • 3
  • 11