Finding a pattern in an array that is not always consistant

I have an ordered data set of decimal numbers. This data is always similar – but not always the same. The expected data is a few, 0 – 5 large numbers, followed by several (10 – 90) average numbers then follow by smaller numbers. There are cases where a large number may be mixed into the average numbers’ See the following arrays.

let expectedData = [35.267,9.267,9.332,9.186,9.220,9.141,9.107,9.114,9.098,9.181,9.220,4.012,0.132];



let expectedData = [35.267,32.267,9.267,9.332,9.186,9.220,9.141,9.107,30.267,9.114,9.098,9.181,9.220,4.012,0.132];

I am trying to analyze the data by getting the average without high numbers on front and low numbers on back. The middle high/low are fine to keep in the average. I have a partial solution below. Right now I am sort of brute forcing it but the solution isn’t perfect. On smaller datasets the first average calculation is influenced by the large number.

My question is: Is there a way to handle this type of problem, which is identifying patterns in an array of numbers?

My algorithm is:

  1. Get an average of the array
  2. Calculate an above/below average value
  3. Remove front (n) elements that are above average
  4. remove end elements that are below average
  5. Recalculate average

In JavaScript I have: (this is partial leaving out below average)

let total= expectedData.reduce((rt,cur)=> {return rt+cur;}, 0);
let avg = total/expectedData.length;
let aboveAvg = avg*0.1+avg;
let remove = -1;
 
for(let k=0;k<expectedData.length;k++) {
    if(expectedData[k] > aboveAvg) {
    remove=k;
    } else {
        if(k==0) {
            remove = -1;//no need to remove
        } 
          //break because we don't want large values from middle removed.
        break;
        }
}
if(remove >= 0 ) {
//remove front above average     
 expectedData.splice(0,remove+1);
 
}

//remove belows
//recalculate average