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.

4
  • 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 stackoverflow.com/help/mcve would help in asking questions that would receive more attention Commented Dec 15, 2017 at 19:36
  • Possible duplicate of JavaScript/Lodash intersection of two strings Commented Dec 15, 2017 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. Commented Dec 15, 2017 at 19:45
  • Set those strings as arrays and then use the solution here: link Commented Dec 15, 2017 at 20:15

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This assumes the original strings don't contain any duplicates.
What does? The express or the post? The expression above does look for dups and I mentioned merging the two strings first.
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.
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.
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
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; } 
     }
    

Comments

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

1 Comment

Please describe your answer little more
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.

Comments

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

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.