Skip to main content

findpeak; import java

package findpeak;

import java.util.ArrayList;
import java.util.List;


public class ArrayPeak
{
    private final int[] ARRAY;
    private final int LEN;
    private static final int IGNORE_FIRST_POSITION = 0;
    private final int IGNORE_LAST_POSITION;

    public ArrayPeak(int[] array)
    {
        this.ARRAY = array;
        this.LEN = this.ARRAY.length;
        this.IGNORE_LAST_POSITION = this.LEN - 1;
    }

    public List<Integer> findPeaks()
    {
        List<Integer> peaks = new ArrayList<>();
        for (int position = 0; position < LEN; position++)
        {
            int output = 0;
            if (position != IGNORE_FIRST_POSITION && position != IGNORE_LAST_POSITION)
            {
                int[] trippleArray =
                {
                    ARRAY[position - 1], ARRAY[position], ARRAY[position + 1]
                };
                output = checkPeak(trippleArray);
            }
            peaks.add(output);
        }

        return filterZeros(peaks);
    }

    private static int checkPeak(int[] inputArray)
    {
        int peak = 0;
        if (inputArray[1] > inputArray[0] && inputArray[1] > inputArray[2])
        {
            peak = inputArray[1];
        }
        return peak;
    }

    private static List<Integer> filterZeros(List<Integer> list)
    {
        List<Integer> filteredList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++)
        {
            if (list.get(i) != 0)
            {
                filteredList.add(list.get(i));
            }
        }
        return filteredList;
    }
}