Caculate the number that repeated more than others,,,

hi

i have a problem,look at below example


1 390
1 382
1 383
1 357
1 1
1 1
1 376
3 24



we need code that says the number that is repeated more than others in left side is 1 that repeated 7 time and minimum number is 3 that repeated 1 time…and in right side 1 is repeated 2 time,then its most repeated and one of other that is repeated one time (doesn’t matter which one) like 24 that repeated one time is 1 time, then its in minimum place of right numbers…

so i have code for this that exist in below


<?php
$time_start = microtime(true);

ini_set('max_execution_time', 700);
echo "<br>" ;

ini_set('memory_limit', '2048M');

$sumpage1=array();
$sumpage2=array();
$avgin;
$avgout;
$t1=0;
$t2=0;
 $file = fopen( "yes.txt", "r" ) or exit ( "Unable to open file!" ) ;
   $str = fgets($file);
  while ( !feof ( $file ) )
    {
       $str = fgets($file);
  $r= explode(" ", $str);
/*  $r[1]=trim($r[1]);
*/  
       if(empty($sumpage1[$r[0]])){
       $sumpage1[$r[0]]=1;
    }
    else {
      $sumpage1[$r[0]]++;
      }
      
    if(empty($sumpage1[$r[1]])){
      $sumpage2[$r[1]]=1;
    echo $r[1];
    echo "<br>";
    }
    else {
      $sumpage2[$r[1]]++;
    echo "dd".$r[1];
    echo "<br>";
      }

  }
 echo $sumpage2[4];
echo "<center>";
echo "<br>";
$x1=max($sumpage1);
$x2=array_search($x1,$sumpage1);
echo "mux number in right: ".$x2." and this number repeated  ".$x1."times";
echo "<br>";
$x11=min($sumpage1);
$x22=array_search($x11,$sumpage1);
echo "min number in right:   : ".$x22." and this number repeated ".$x11."times";

echo "<br><br><br>";

$y1=max($sumpage2);
$y2=array_search($y1,$sumpage2);
echo "mux number in left: ".$y2." and this number repeated".$y1."times";
echo "<br>";
$y11=min($sumpage2);
$y22=array_search($y11,$sumpage2);
echo "min number in left: ".$y22."and this number repeated ".$y11."times";

echo "<br>";
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "proccess time: {$time}";
echo "</center>";
?>

whats your point?

Solved

That’s great. Would you mind telling us what the problem was and how you solved it? It might be interesting for others if they encounter the same problem in the future.

1 Like

Code was right but
and i had one error about first element that goes to array, i got error about undefined offset:

but answer was right,when no one answered me for this,i used ‘@’ for ignoring error in $r[1];

and final code is below


<?php
$time_start = microtime(true);

ini_set('max_execution_time', 700);
echo "<br>" ;

ini_set('memory_limit', '2048M');
$startmem = memory_get_usage(); 
$sumpage1=array();
$sumpage2=array();

 $file = fopen( "web-graph.txt", "r" ) or exit ( "Unable to open file!" ) ;
   $str = fgets($file);
  while ( !feof ( $file ) )
    {
       $str = fgets($file);
  $r= explode(" ", $str);
/*  $r[1]=trim($r[1]);
*/  
       if(empty($sumpage1[$r[0]])){
       $sumpage1[$r[0]]=1;
    }
    else {
      $sumpage1[$r[0]]++;
      }
      
    if(empty($sumpage2[@$r[1]])){
      $sumpage2[@$r[1]]=1;
    }
    else {
      $sumpage2[$r[1]]++;
      }
  }

echo "<br>";
$x1=max($sumpage1);
$x2=array_search($x1,$sumpage1);
echo "max number in right: ".$x2." and this number repeated  ".$x1."times";
$x11=min($sumpage1);echo "<br>";
$x22=array_search($x11,$sumpage1);
echo "min number in right:   : ".$x22." and this number repeated ".$x11."times";

echo "<br><br><br>";

$y1=max($sumpage2);
$y2=array_search($y1,$sumpage2);
echo "max number in left: ".$y2." and this number repeated".$y1."times";
echo "<br>";echo "<br>";
$y11=min($sumpage2);
$y22=array_search($y11,$sumpage2);
echo "min number in left: ".$y22."and this number repeated ".$y11."times";
$mem = memory_get_usage()-$startmem;
echo "<br>";
echo 'Memory:'.$mem."\n\n"; 
echo "<br>";
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "proccess time: {$time}";
echo "</center>";
?>

In my opinion suppressing error(s) isn’t the “correct” way in solving the problem. What happens down the road when you add or modify the code? Even worse when someone else is trying to debug, modify or add to it? Answer - It’s going to make it all that much worse.

1 Like

yes,but its only solution for me now,

this error did not changed the results,and results was right,

As far s you know.

I avoid error suppression like the plague.

Some would argue that it acceptable when the code is relying on something to happen that on rare occasion might not happen and is beyond their control eg. an API call to a remote website that might go down once a year.

But I have found that in most, if not all cases, all that is needed is to write a few more lines of code to deal with the possibility and that error suppression was used as a type of short-cut instead of writing more robust code.

For your code what it does is not anything beyond your control.

You have an array,
These array values are used as keys in another array.
If there is a problem with the initial array item, it causes an error when an attempt is made to use it as a key in the second array.

All you need to do is test to make sure there is no problem with the initial array item before you attempt to use it as a key for the second array.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.