Jump to content

array


bagnallc

Recommended Posts

Hi there,

There are so many array functions and im gradually trying to learn them.

For my current purpose i am trying to identify the longest number of occurences when something did not happen. The best way i can explain this is with an example.

Say i have the following - 1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 1, 3

and i want to return the fact that the longest sequence of numbers without 1 appearing was seven (when the chain was 5, 6, 7, 8, 8, 8, 9)

is there an easy way of doing this that you can think of?

many thanks

chris


Link to comment
Share on other sites

use a for loop over every character in the array, set the number/character you want to find the largest run without, i.e. in your example you would supply "1"

your for loop iterates over the array looking for "1", all the while adding up a counter, when the next time the "1" appears it stores the counter vairiable, it then resets the counter and starts counting again, when it finds another "1" it checks the counter value against the stored count value and saves whichever is the largest, so on so on untillt he array is finished then prinbtes out the stored counter value.

make sense??
Link to comment
Share on other sites

I used this
[code]
<?PHP
$array = array(1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 1, 3);
$value = 1;                                              //the value you want to find out between i.e largest count between "1"'s
$answer = 0;                                            //initialise the answer
$count = 0;                                              //initialise the counter
for ($i = 0; $i < count($array); $i++) {          //set up the for loop based on the array elements count
$char = $array[$i];                                      //assign each value to the $char variable
if ($char == $value) {                                //check to see if it is the same as the supplied data i.e  1
    if ($count > $answer) {                      //if it is, check to see if the new value of count is larger than the stored value of answer
      $answer = $count;                                //if yes make the asnwer the same as count
      }
    $count = 0; //then reset the counter
}
else {
$count++;                                              //if it is not the same then increase the value of the counter
}
}

echo $answer;  //write out the answer

?>
[/code]

and got the following solution

[code]
7
[/code]

which is correct....
Link to comment
Share on other sites

or make it a function
[code]
function countArray($array,$value){
$answer = 0;
$count = 0;
for ($i = 0; $i < count($array); $i++) {
$char = $array[$i];
if ($char == $value) {
    if ($count > $answer) {
      $answer = $count;
      }
    $count = 0;
}
else {
$count++;
}
}
return $answer;
}
[/code]
Link to comment
Share on other sites

Actually guys ive struck a bit of a problem with this!

If the array was changed to

$array = array(1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 3);

(ive taken the last one off)

then the answer returned is 5, when it should be 8. This seems to be as it is the last sequence of numbers in the array.

im playing around with it but havent had any joy as of yet??
Link to comment
Share on other sites

this should do it
[code=php:0]
function codeMode($arr, $value) {
        $count = 0;
foreach($arr as $val) {
    if($val == $value)
      $count = 0;
    else
      $count++;
}
return $count;
}
[/code]

EDIT: wait..no it won't gimme a sec

here
[code=php:0]
function codeMode($arr, $value) {
        $count = array();
$i = 0;
foreach($arr as $val) {
    if($val == $value)
      $i++;
    else {
      $count[$i]++;
}
}
rsort($count);
return $count[0];
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.