How to eliminate duplicate values in an array

I have a sensor which I am recording samples from. I am trying to find local max's within 256 samples of the data. I am storing the samples into an array and looking through the array to identify the local max's. unfortunately it is returning the same max's when the value next to it is equal (see below).

IE: Next max of 456 at index 45
Next max of 456 at index 46
Next max of 456 at index 47
Next max of 456 at index 48
Next max of 456 at index 49

I am looking for a way to compare those values and somehow return the value in the middle and only that value along with its corresponding index. (Next max of 456 at index 47) and when there are even amount of equal values the index can be index.5 or whatever is possible. What can I do?

int inp[256];

void setup()
{
  Serial.begin(9600);  // for debugging  
}
void loop()
{
    int max_i = 0;
    int max_v = 0;
    
    for (int i = 0; i < 256; i++)
    {
     if ( inp[i] > max_v )
      {
       max_v = inp[i];
       max_i = i;
      }
    }
    Serial.println((String)"\nGlobal Max is " + max_v + " at index " + max_i);
    //Serial.println(max_v);

    int i;
    for (i = 0 ; i < 256 ; i ++) // save 256 samples
    {     
      inp[i] = analogRead(A0);
      delay(10);
    } // close for
   
  int N = 15;   // loc max neighborhood size
  for (int i = N-1; i < 255-N; i++) 
  {
      bool loc = true;
      for (int j = 1; j < N; j++) // look N-1 back and N-1 ahead
      { 
          if (inp[i] < inp[i-j] || inp[i] < inp[i+j]) loc = false;
          }

      if (loc == true) 
      {   
      Serial.println((String)"Next max of " + inp[i] + " at index " + i);
      }
      //Serial.println(inp[i]);          
  } 
  Serial.println("-------------");   
   } // close main loop

Remember the first occurence of a max value, and its last occurence in sequnce. Then take the index between both readings.

To eliminate identical values in an array, sort the array first.
Then work your way through the array, eliminating the second of two adjacent identical values, and move up the rest of the entries.

You appear to be doing something similar to analyzing a waveform and identifying peaks, but not doing any smoothing.

How are you going to handle such a data stream:
400, 420, 440, 439, 440, 420, 400

Are you going to identify the two values of 440 as separate peaks, or are you going to regard the three samples 440, 439, 440 as a combined peak ?

'i' is going to take values of 14...240 so your algorithm will not find maximum values in the array outside that range of indexes. Is that intended e.g. the highest value in the whole array might be at inp[10] ?

To get rid of consecutive repeat readings, you could create a second array which notes the index number at which the value in the main array changes. Then don't process the main array one cell at a time, but skip to each change point.

Perhaps some of you can enlighten me with some sample code, I'm still not quite getting fully what to do here, your answers are greatly appreciated.

#include <stdio.h>
#include <stdbool.h>

#define ARRAY_SIZE 20
#define INDEX 0
#define VALUE 1
#define COUNT 2

int main()
{
    int inp[ARRAY_SIZE]=
    {
        240,100,150,155,157,157,157,140,140,170,170,170,180,160,190,100,200,100,100,120
    };
    int compressed[ARRAY_SIZE][3];
    int used;
    bool rising;
    
    used=1;
    compressed[0][INDEX]=0;
    compressed[0][VALUE]=inp[0];
    compressed[0][COUNT]=1;
    
    for(int i=1;i<ARRAY_SIZE;++i)
    {
        if(inp[i]==compressed[used-1][VALUE])
        {
            ++compressed[used-1][COUNT];
        }
        else
        {
            compressed[used][INDEX]=i;
            compressed[used][VALUE]=inp[i];
            compressed[used][COUNT]=1;
            ++used;
        }
    }
    
    for(int i=0;i<used;++i)
    {
        printf("Index %02d, Value %03d, Count %02d\n",compressed[i][INDEX],compressed[i][VALUE],compressed[i][COUNT]);
    }
    printf("\n");
    
    
    rising=true;
    for(int i=0;i<used-1;++i)
    {
        if(rising&&compressed[i][VALUE]>compressed[i+1][VALUE])
        {
            printf("Next max of %03d at index %03d\n",compressed[i][VALUE],compressed[i][INDEX]);
            rising=false;
        }
        else
        {
            rising=true;
        }
    }
    if(compressed[used-1][VALUE]>compressed[used-2][VALUE])
    {
        printf("Next max of %03d at index %03d\n",compressed[used-1][VALUE],compressed[used-1][INDEX]);
    }
    printf("\n");
    

    return 0;
}
Index 00, Value 240, Count 01
Index 01, Value 100, Count 01
Index 02, Value 150, Count 01
Index 03, Value 155, Count 01
Index 04, Value 157, Count 03
Index 07, Value 140, Count 02
Index 09, Value 170, Count 03
Index 12, Value 180, Count 01
Index 13, Value 160, Count 01
Index 14, Value 190, Count 01
Index 15, Value 100, Count 01
Index 16, Value 200, Count 01
Index 17, Value 100, Count 02
Index 19, Value 120, Count 01

Next max of 240 at index 000
Next max of 157 at index 004
Next max of 180 at index 012
Next max of 190 at index 014
Next max of 200 at index 016
Next max of 120 at index 019