1

I have a sub-array and i am trying to find to the number of times it is repeated in the main array.

I got to the point where i can determine if it is a sub-array but cannot take it further. Any ideas how i can do that in JavaScript.

    function isSubArray(main_array, sub_array)
    {
        var i, j;
        for (i=0,j=0; i<main_array.length && j<sub_array.length;) 
        {
           if (main_array[i] !== sub_array[j]) 
           {
              ++i;
           }
           else if (main_array[i] === sub_array[j])
           {
              ++i; ++j;
           }
        }
        if(j == sub_array.length)
        {
           return true;
        }
        else
        {
           return false;
         }
     }

Example:

array = 1,2,3,4,5,1,2,3 sub_array = 1,2,3

The sub_array repeats in the main array 2 times

Mahima
  • 65
  • 1
  • 8
  • 1
    Please [edit] your question to show some code - at minimum show an example input and the corresponding desired output. When you talk about a "sub-array" being repeated, are you talking about nested arrays, or something like `[1,2,3,1,2,3]` repeating the `1,2,3` values? – nnnnnn Feb 15 '17 at 02:44
  • Can you please post what you have done so far? – Dana Feb 15 '17 at 02:44
  • try to post the array (if it's not overwhelmingly big) and what you're doing to try to get the subarray – A. L Feb 15 '17 at 02:47
  • Answered and posted my implementation of the subarray function @nnnnnn – Mahima Feb 15 '17 at 06:29
  • 1
    What if there are overlaps? As in, how many times does `[1,2,1]` repeat in `[1,2,1,2,1,2,1,2,1]`? – nnnnnn Feb 15 '17 at 08:48
  • @nnnnnn for my implementation i know the number are unique. But as far as your example goes. In my case there will be 2. – Mahima Feb 15 '17 at 18:24

2 Answers2

1

Here is a dynamic function made by me to get your required solution,

I have taken two different arrays and returned the repeated count of both dynamic.

var array = [1,2,3,4,5,1,2,3];
var array1 = [1,2,3,4,5,1,2,3,5,9,1,2,3];
var sub_array = [1,2,3];

function getRepeatedCount(array,sub_array)
{
    var count = 0;
    for(i = 0; i < array.length; i++  )
    {
        // console.log((array.slice(i,i + sub_array.length)) === sub_array)
        var repeated = ((array.slice(i,i + sub_array.length)).length==sub_array.length && (array.slice(i,i + sub_array.length)).every(function(v,i) { return v === sub_array[i]}))
        if(repeated)
        {
            count += 1;
        }
    }
    return count;
}
console.log("Array1",array, "Sub Array", sub_array, "Repeated count -> ",getRepeatedCount(array,sub_array));
console.log("Array1",array1, "Sub Array", sub_array, "Repeated count -> ",getRepeatedCount(array1,sub_array));

PLEASE RUN THE ABOVE SNIPPET

Procedure:

I created a function, which slices the main array into chunks(parts) that equals the length of the subarray and compare each chunk of small array with the subarray.

If the chunk(part) equals the sub_array, then a count variable is incremented in the function and gets returned.

HERE IS A WORKING DEMO

Thus, I made the function dynamic so that you can call it as many times you want with different arrays and sub_arrays.

Sravan
  • 18,467
  • 3
  • 30
  • 54
0
1) Find the position of the first occurrence of the sub array in the main array.
2) slice() off that bit of the main array.
3) repeat until you find no more occurrences of the sub array in the main array.
4) Count how many times you had to do what.

Details are left as an exercise for the questioner :-)

Ray Fischer
  • 936
  • 7
  • 9