Count pairs in an array which have at least one digit common | Returns true if the pair is valid , otherwise false ; converting integers to strings ; Iterate over the strings and check if a character in first string is also present in second string , return true ; No common digit found ; Returns the number of valid pairs ; Iterate over all possible pairs ; Driver Code to test above functions
function checkValidPair ( num1 , num2 ) { var s1 = num1 . toString ( ) ; var s2 = num2 . toString ( ) ; var i , j ; for ( i = 0 ; i < s1 . length ; i ++ ) for ( j = 0 ; j < s2 . length ; j ++ ) if ( s1 [ i ] == s2 [ j ] ) return true ; return false ; } function countPairs ( arr , n ) { var numberOfPairs = 0 ; for ( i = 0 ; i < n ; i ++ ) for ( j = i + 1 ; j < n ; j ++ ) if ( checkValidPair ( arr [ i ] , arr [ j ] ) ) numberOfPairs ++ ; return numberOfPairs ; } var arr = [ 10 , 12 , 24 ] ; var n = arr . length ; ; document . write ( countPairs ( arr , n ) ) ;