Jump to content

What are the most 3 repeated numbers in a string


npsari

Recommended Posts

Hello there

 

I have a string like that...

 

$string = "224 556 665 224 667 443 776 443 222 443 667 224 997 665 009 443 443";

 

I want to use a PHP script to echo the most 3 repeated numbers in this string

 

So, the result should be...

 

443 224 665

 

Do you know how can i do that in PHP

 

 

Well, I don't know if Mchl's answer works for you, but I wanted to see if I could get the Mode (that what it is called in math) three times. Here it is

<?php
$string = array(224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443);
function M_MODE($array){
ksort($array);
$Sets = array();
foreach($array as $number){
	if(in_array($number, $array)){
		$Sets[$number]++;
	}
	}
arsort($Sets);
echo key($Sets)." ";
next($Sets);
echo key($Sets)." ";
next($Sets);
echo key($Sets)." ";
}
M_MODE($string);
?>

It probably sucks but it works and I'm proud of it. lol

Well... I'd go with something along these lines

 

$string = "224 556 665 224 667 443 776 443 222 443 667 224 997 665 009 443 443";

$stringArray = explode(" ",$string); //convert string to array

$countValues = array_count_values($stringArray);  //count values in array

arsort($countValues);  //sort in descending order maintaining key=>value association

$result = array_slice($countValues,0,3,TRUE); //get three first elements from $countValues

print_r($result);

 

 

Thank you both

 

though, there is somethign weird here

 

I changed a tiny bit in the first line

 

<?php
$numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443";
$string = array($numbers);
function M_MODE($array){
ksort($array);
$Sets = array();
foreach($array as $number){
	if(in_array($number, $array)){
		$Sets[$number]++;
	}
	}
arsort($Sets);
echo key($Sets)." ";
next($Sets);
echo key($Sets)." ";
next($Sets);
echo key($Sets)." ";
}
M_MODE($string);
?>

 

It doesnt work anymore, whyyy

The problem is that this

<?php
$numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443";
$string = array($numbers);
?>

is not the same as

<?php
$string = array(224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443);
?>

The first code creates an array with one entry that is the string. The second one creates an array of all the numbers.

 

Ken

Thank you Ken

 

Is it possible that you tell me how to do this, because i can't succeed

 

I am trying to get the results as strings

 

arsort($Sets);
echo key($Sets)." ";
next($Sets);
$get1 = key($Sets)." ";
next($Sets);
$get2 = key($Sets)." ";

 

Because i want to use $get1 & $get2 & $get3 later on

 

What i done above doesn't work

The problem is that $get1, $get2 & $get3 are set in the function, so they are local to the function. To use them outside, you have to return the values at the end of the function.

 

<?php
function mode($set) {
   $cv = array_count_values($set);
   arsort($cv);
   $result = array_slice($cv,0,3,TRUE);
   print_r(array_keys($result));
   return(array_keys($result));
}
$numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443";
list($get1,$get2,$get3) = mode(explode(',',$numbers));
echo $get1 . "<br>";
echo $get2 . "<br>";
echo $get3 . "<br>";
?>

 

Ken

:) Is this a new way of doing it, because i am using the method provided by Brian

 

Anyway, I uploaded your script in an empty php page, i got the following error...

 

Warning: Wrong parameter count for array_slice() in /home/zortins/public_html/try.php on line 5

 

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/zortins/public_html/try.php on line 6

 

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/zortins/public_html/try.php on line 7

 

Is it possibe to show me how can i do it in Brian's script (Because i already added to it parts of mine)

 

Can you tell me how can i have $get1 $get2 $get3 outside that function

 

 

What i have now is this:

 

$get_string = "224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443";

$string = explode(',',$get_string );

function M_MODE($array){
ksort($array);
$Sets = array();
foreach($array as $number){
	if(in_array($number, $array)){
		$Sets[$number]++;
	}
	}
arsort($Sets);
echo key($Sets)." ";
next($Sets);
$get1 = key($Sets);
next($Sets);
$get2 = key($Sets);
next($Sets);
$get3 = key($Sets);

}
M_MODE($string);

print "$get1 - $get2 - $get3";

What version of php are your running? My script works perfectly with PHP 5.2.5

 

With the other routine:

<?php
function M_MODE($array){
ksort($array);
$Sets = array();
foreach($array as $number){
	if(in_array($number, $array)){
		$Sets[$number]++;
	}
}
arsort($Sets);
        $result = array_slice($Sets,0,3,TRUE);
        return(array_keys($result));
}
$numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443";
list($get1,$get2,$get3) = M_MODE(explode(',',$numbers));
echo $get1 . "<br>";
echo $get2 . "<br>";
echo $get3 . "<br>";
?>

 

Ken

To tell you the truth, I dont know about php versions, all i know is PHP!

 

I am a Mechanical Engineer, programming is a hobby to me

 

Thanks for helping me by the way

 

So anyway, i uploaded this new script you just given me in an empty page

 

I got that error...

 

Warning: Wrong parameter count for array_slice() in /home/zortins/public_html/try.php on line 11

 

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/zortins/public_html/try.php on line 12

 

So, you took Brian's script and shaped it differently, is that what you have done here

change the routine to:

<?php
function M_MODE($array){
        ksort($array);
        $Sets = array();
        foreach($array as $number){
                if(in_array($number, $array)){
                        $Sets[$number]++;
                }
        }
        arsort($Sets);
        $result = array_slice(array_keys($Sets),0,3);
        return($result);
}
$numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443";
list($get1,$get2,$get3) = M_MODE(explode(',',$numbers));
echo $get1 . "<br>";
echo $get2 . "<br>";
echo $get3 . "<br>";
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.