diff --git a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java index 29956ba027..f8e13ee86e 100644 --- a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java +++ b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java @@ -12,11 +12,11 @@ public class MultiplePeakFinder { if (arr == null || arr.length == 0) { return peaks; } - findPeakElement(arr, 0, arr.length - 1, peaks); + findPeakElements(arr, 0, arr.length - 1, peaks, arr.length); return peaks; } - private static void findPeakElement(int[] arr, int low, int high, List peaks) { + private static void findPeakElements(int[] arr, int low, int high, List peaks, int length) { if (low > high) { return; @@ -24,17 +24,17 @@ public class MultiplePeakFinder { int mid = low + (high - low) / 2; - if ((mid == 0 || arr[mid] >= arr[mid - 1]) && (mid == arr.length - 1 || arr[mid] >= arr[mid + 1])) { - peaks.add(arr[mid]); - } - - if (mid > 0) { - findPeakElement(arr, low, mid - 1, peaks); - } - - if (mid < arr.length - 1) { - findPeakElement(arr, mid + 1, high, peaks); + boolean isPeak = (mid == 0 || arr[mid] > arr[mid - 1]) && (mid == length - 1 || arr[mid] > arr[mid + 1]); + boolean isFirstInSequence = mid > 0 && arr[mid] == arr[mid - 1] && arr[mid] > arr[mid + 1]; + + if (isPeak || isFirstInSequence) { + + if (!peaks.contains(arr[mid])) { + peaks.add(arr[mid]); + } } + findPeakElements(arr, low, mid - 1, peaks, length); + findPeakElements(arr, mid + 1, high, peaks, length); } }