Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong><a href="http://jsfiddle.net/kCqaG/1/" rel="nofollow">Here is my solution</a></strong>, much like @robert king 's (as I discovered after tackling the problem myself) except mine is complete (already is capable of not counting overlapping patterns) and optimised (as much as I can).</p> <p>Also, returns a map of objects, so you can enumerate over it and only pull out patterns of size X, or ones that repeated Y times etc.</p> <p><br/> The following line (with the below function)</p> <pre class="lang-js prettyprint-override"><code>getPatterns([1,2,1,1,1,1,0,2,1,2,3,10,12,54,10,12]).showRepeated(); </code></pre> <p>will result in this;</p> <pre class="lang-js prettyprint-override"><code>1 2 found 2 times 2 1 found 2 times 1 1 found 2 times 10 12 found 2 times </code></pre> <p><br/> <strong>CODE</strong></p> <pre class="lang-js prettyprint-override"><code>function getPatterns(input, generateAll) { var patternMap = new getPatterns.presentation(); var generated = []; var patternObj; var start; //for each item for (var index = 0; index &lt; input.length; ++index) { //open a new slot for a new pattern start at this index generated.push(''); start = 0; //unless told to generate all //skip patterns that cant possibly be repeated //(i.e. longer than half the input length) if (!generateAll &amp;&amp; generated.length &gt; input.length / 2) start = generated.length - Math.floor(input.length / 2); //test patterns we have generated for this index for (var index2 = start; index2 &lt; generated.length; ++index2) { //generate a fresh lot of patterns for this index generated[index2] += ' ' + input[index]; //unless told to generate all, dismiss patterns of length 1 if (!generateAll &amp;&amp; index2 == generated.length - 1) break; //try to fetch a pre-existing pattern, O(1) patternObj = patternMap[generated[index2]]; //if this is a new pattern if (!patternObj) { //generate an object patternMap[generated[index2]] = { lastSeen : index, count : 1, size : generated.length - index2 }; continue; } //unless told to generate all, skip patterns that overlap with themselves if (!generateAll &amp;&amp; index - patternObj.lastSeen &lt; patternObj.size) continue; //this pattern has repeated! update the object data ++patternObj.count; patternObj.lastSeen = index; } } return patternMap; } //just for a function prototype getPatterns.presentation = function() {}; getPatterns.presentation.prototype = { showRepeated : function() { var patternObj; for (var pattern in this) { patternObj = this[pattern]; if (patternObj.count &gt; 1) console.log(pattern + '\tfound ' + patternObj.count + ' times'); } } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload