Regex for requiring at least 3 different charecters?

Travis Michigan
Mega Sage

I'm trying to get a regex for a masked field that I want to require that there are at least 3 different characters in the entry.  Does anyone have a suggestion?

So an entry of just 1s and 2s would fail, like '1221121' but a third char would pass '1221123'

1 ACCEPTED SOLUTION

Community Alums
Not applicable

@Travis Michigan 
Please try the below snippet:

function hasAtLeastThreeUniqueCharacters(input) {
    var uniqueCharacters = {}; // store unique characters
    var count = 0; // Counter for unique characters

    for (var i = 0; i < input.length; i++) {
        var character = input[i];
        if (!uniqueCharacters.hasOwnProperty(character)) {
            uniqueCharacters[character] = true; // Mark this character as encountered
            count++; // Increment count for each unique character
        }
    }
    return count >= 3; // Check if there are at least three unique characters
}

// Example usage
gs.print(hasAtLeastThreeUniqueCharacters('1221123'));    // true
gs.print(hasAtLeastThreeUniqueCharacters('1221121')); // false
gs.print(hasAtLeastThreeUniqueCharacters('1234567')); // true
gs.print(hasAtLeastThreeUniqueCharacters('aabbcc'));  // true
gs.print(hasAtLeastThreeUniqueCharacters('aaaaabb'));   // false
gs.print(hasAtLeastThreeUniqueCharacters('abcde'));  // true

The above method is to get the count of distinct characters and if its greater or equal to 3 it will return true.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar Gupta

 

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

@Travis Michigan 
Please try the below snippet:

function hasAtLeastThreeUniqueCharacters(input) {
    var uniqueCharacters = {}; // store unique characters
    var count = 0; // Counter for unique characters

    for (var i = 0; i < input.length; i++) {
        var character = input[i];
        if (!uniqueCharacters.hasOwnProperty(character)) {
            uniqueCharacters[character] = true; // Mark this character as encountered
            count++; // Increment count for each unique character
        }
    }
    return count >= 3; // Check if there are at least three unique characters
}

// Example usage
gs.print(hasAtLeastThreeUniqueCharacters('1221123'));    // true
gs.print(hasAtLeastThreeUniqueCharacters('1221121')); // false
gs.print(hasAtLeastThreeUniqueCharacters('1234567')); // true
gs.print(hasAtLeastThreeUniqueCharacters('aabbcc'));  // true
gs.print(hasAtLeastThreeUniqueCharacters('aaaaabb'));   // false
gs.print(hasAtLeastThreeUniqueCharacters('abcde'));  // true

The above method is to get the count of distinct characters and if its greater or equal to 3 it will return true.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar Gupta

 

Thank you @Community Alums !