Compare two integer and count matching numbers

BurningSimon

I have two integers for example "12345" and "98754", they have a count of 2 matching numbers namely 4 and 5, the order doesnt matter.

Now: How do I check something like that in PHP?

Matthew Knight
  1. Convert the numbers in to strings
  2. create a loop from 0-9 to check for the appearance of a number in both strings using strstr() or similar
  3. store the number in an array if it appears in both

Edit: Code-centric solution:

$a = 1231534;
$b = 89058430;


    $matches = compare( $a, $b );   
    print count($matches);


    function compare ( $a, $b ) {
        $str_a = (string) $a;
        $str_b = (string) $b;
        $matches = [];
        for($i=0;$i<=9;$i++) {
            if (strstr($str_a, (string)$i) && strstr($str_b,(string)$i)) $matches[] = $i;
        }

        return $matches;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare two dataframe columns for matching strings or are substrings then count in pandas

Compare rows of two dataframes to find the matching column count of 1's

Compare Django Count() to Integer

Compare two numbers, ramdajs

Compare two large numbers

Compare two hex numbers

Compare two numbers in a QString

Compare two VAT numbers

Python If statements (or any statement) to compare two columns based on two conditions (integer numbers)

How do I compare two lists and remove matching numbers from the second one?

Compare two dataframe columns for matching strings or are substrings then count in pandas (Need for speed edition)

Matching two integer vectors in R

compare two Integer objects with == in java

Compare two files by two column matching

Trying to compare two columns that are numbers

Oracle compare two numbers in select

How to compare two complex numbers?

Compare two variables with generated numbers

Compare 2 int arrays for matching numbers - java

Compare values of a dictionary and return a count of matching values

Compare values in GroupBy and count the matching rows

Compare 2 Integer numbers, strange behavior

Python compare integer number with hexadecimal and binary numbers

Function for random integer of two numbers and not between to numbers

Compare two columns of two files and count the differences

How to write dynamic Linq to count matching numbers

Compare dataframes and output the numbers of count matches

Compare Two columns If 2 cells are Matching

Compare two large files and combine matching information