Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:16:38+00:00 2026-05-16T08:16:38+00:00

Array (3, 5, 1, 3, 5, 48, 4, 7, 13, 55, 65, 4, 7,

  • 0

Array (3, 5, 1, 3, 5, 48, 4, 7, 13, 55, 65, 4, 7, 13, 32)

the frequent sequence of numbers will be (3, 5) f=2 + (4, 7, 13) f=2

any Algorithm or Pseudo code to find that ?

Update(1):

if (7, 13) also occurrence it will be included in the longest one by update its frequency so

(4, 7, 13) f=3 and so on…

Update(2):

in case of (1,2,3,4,1,2,3,4,1,2,7,8,7,8,3,4,3,4,1,2) the output should be (1,2,3,4) & (3,4,1,2)

& (7,8) , to make it clear consider each number as a word and you want to find most frequent phrases

so it is common to see same word(s) in a lot of phrases but if any phrase was sub-string for any other

phrase(s) should not be consider as a phrase but will update frequency of each phrase includes it

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:16:38+00:00Added an answer on May 16, 2026 at 8:16 am

    ** EDIT ** : slightly better implementation, now also returns frequences and has a better sequence filter.

    function getFrequences($input, $minimalSequenceSize = 2) {
       $sequences = array();
       $frequences = array();
    
       $len = count($input);
       for ($i=0; $i<$len; $i++) {
          $offset = $i;
    
          for ($j=$i+$minimalSequenceSize; $j<$len; $j++) {
             if ($input[$offset] == $input[$j]) {
                $sequenceSize = 1;
                $sequence = array($input[$offset]);
                while (($offset + $sequenceSize < $j) 
                   && ($input[$offset+$sequenceSize] == $input[$j+$sequenceSize])) {
    
                   if (false !== ($seqIndex = array_search($sequence, $frequences))) {
                      // we already have this sequence, since we found a bigger one, remove the old one
                      array_splice($sequences, $seqIndex, 1);
                      array_splice($frequences, $seqIndex, 1);
                   }              
    
                   $sequence[] = $input[$offset+$sequenceSize];
                   $sequenceSize++;
                }
    
                if ($sequenceSize >= $minimalSequenceSize) {
                   if (false !== ($seqIndex = array_search($sequence, $sequences))) {
                      $frequences[$seqIndex]++;
                   } else {
                      $sequences[] = $sequence;
                      $frequences[] = 2;  // we have two occurances already
                   }
                   // $i += $sequenceSize;  // move $i so we don't reuse the same sub-sequence
                   break;
                }
             }
          }
       }
    
       // remove sequences that are sub-sequence of another frequence
       // ** comment this to keep all sequences regardless ** 
       $len = count($sequences);
       for ($i=0; $i<$len; $i++) {
          $freq_i = $sequences[$i];
          for ($j=$i+1; $j<$len; $j++) {
             $freq_j = $sequences[$j];
             $freq_inter = array_intersect($freq_i, $freq_j);
             if (count($freq_inter) != 0) {
                $len--;
                if (count($freq_i) > count($freq_j)) {
                   array_splice($sequences, $j, 1);
                   array_splice($frequences, $j, 1);
                   $j--;
                } else {
                   array_splice($sequences, $i, 1);
                   array_splice($frequences, $i, 1); 
                   $i--;
                   break;
                }
             }
          }
       }
    
       return array($sequences, $frequences);
    };
    

    Test case

    header('Content-type: text/plain');
    
    $input = array(3, 5, 1, 3, 5, 48, 4, 7, 13, 55, 3, 5, 65, 4, 7, 13, 32, 5, 48, 4, 7, 13);
    
    list($sequences, $frequences) = getFrequences($input);
    foreach ($sequences as $i => $s) {
       echo "(" . implode(',', $s) . ') f=' . $frequences[$i] . "\n";
    }
    

    ** EDIT ** : here’s an update to the function. It was almost completely rewritten… tell me if this is what you were looking for. I also added a redundancy check to prevent counting the same sequence, or subsequence, twice.

    function getFrequences2($input, $minSequenceSize = 2) {
      $sequences = array();
    
      $last_offset = 0;
      $last_offset_len = 0;
    
      $len = count($input);
      for ($i=0; $i<$len; $i++) {
         for ($j=$i+$minSequenceSize; $j<$len; $j++) {
            if ($input[$i] == $input[$j]) {
               $offset = 1;
               $sub = array($input[$i]);
               while ($i + $offset < $j && $j + $offset < $len) {
                  if ($input[$i + $offset] == $input[$j + $offset]) {
                     array_push($sub, $input[$i + $offset]);
                  } else {
                     break;
                  }
                  $offset++;
               }
    
               $sub_len = count($sub);
               if ($sub_len >= $minSequenceSize) {
                  // $sub must contain more elements than the last sequence found
                  // otherwise we will count the same sequence twice
                  if ($last_offset + $last_offset_len >= $i + $sub_len) {
                     // we already saw this sequence... ignore
                     continue;
                  } else {
                     // save offset and sub_len for future check
                     $last_offset = $i;
                     $last_offset_len = $sub_len;
                  }
    
                  foreach ($sequences as & $sequence) {
                     $sequence_len = count($sequence['values']);
                     if ($sequence_len == $sub_len && $sequence['values'] == $sub) {
                        //echo "Found add-full ".var_export($sub, true)." at $i and $j...\n";
                        $sequence['frequence']++;
                        break 2;
                     } else {
                        if ($sequence_len > $sub_len) {
                           $end = $sequence_len - $sub_len;
                           $values = $sequence['values'];
                           $slice_len = $sub_len;
                           $test = $sub;
                        } else {
                           $end = $sub_len - $sequence_len;
                           $values = $sub;
                           $slice_len = $sequence_len;
                           $test = $sequence['values'];
                        }
                        for ($k=0; $k<=$end; $k++) {
                           if (array_slice($values, $k, $slice_len) == $test) {
                              //echo "Found add-part ".implode(',',$sub)." which is part of ".implode(',',$values)." at $i and $j...\n";
                              $sequence['values'] = $values;
                              $sequence['frequence']++;
                              break 3;
                           }
                        }
                     }
                  }
    
                  //echo "Found new ".implode(',',$sub)." at $i and $j...\n";
                  array_push($sequences, array('values' => $sub, 'frequence' => 2));
                  break;
               }
            }
         }
      }
    
      return $sequences;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A frequent question that props up during array manipulation exercises is to rotate a
Find the nth most frequent number in array. (There is no limit on the
I have an array of data that is returned as a JSON response. The
Frequent visitor but first post here on StackOverflow, I'm hoping that you guys might
I've been writing a program that can determine any and every data about an
I have an function that creates an array of words from a string, counts
Array ( [0] => myemail@domain.com [1] => mysecondemail@domain.com [2] => invalidEmail.com ) Notice that
Array only extends the non-generic version of IEnumerable, so will there be boxing and
I have this Array i wrote a function MostFreq that takes an array of
I've got a 2D array of int's in a console c# code (7X3 'cells').

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.