[Question]
  [
**This question already has answers here**:
 
 
[Finding Local Extremes](/questions/22252/finding-local-extremes)
 (21 answers)
 
Closed 3 years ago.
Given an array of positive integers, output an array of all the elements that are greater than or equal to the adjacent ones. Most elements will have two adjacent elements; the first and last element are special cases, as they only have one adjacent element.
You may assume that the array contains at least two elements.
Test cases:
```
Input               | Output
[4,2,6,12,4,5,4,3]  | [4,12,5]
[1,2]               | [2]
[1,2,3,2,1]         | [3]
[3,2,1,2,3]         | [3,3]
[4,4]               | [4,4]
[2,4,4,4,1]         | [4,4,4]
[2,3,3,4]           | [3,4]
[4,3,3,4]           | [4,4]
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), shortest code wins!
  
[Answer]
# [Python](https://docs.python.org/2/), 54 bytes
```
f=lambda l,*p:l and l[:p<=l[:1]>=l[1:2]]+f(l[1:],l[0])
```
[Try it online!](https://tio.run/##bZDBbsMgDIbvPIWVS8JmTYPQHqKxF2EcmNpokYCiJm01qe@e2WyTFmkgG/v/xC9w@Vw@Tlmv62hjSO@HABEfyhAh5ANEN5QXS1n5VzrUoL1/HDuuPEb37OU626ZpnEGNe1QaDe4oeg9wB1JJ2XnhFGpSNouw/ibYUyj/l/REqsp0S5CZQfOPH6vC8Rt4bx2rVik5bG9XV@PpG0KMpzPcYMowP80lTkvXvuVWDgJoKsmmULrjNUS8/dJ7KyVDu1xKPHaRm/TTJG7KecoL0MiktQnJpdbrFw "Python 2 – Try It Online")
I/O is with tuples rather than lists.
---
**[Python](https://docs.python.org/2/), 57 bytes**
```
f=lambda l,p=0:l and l[:[p]<=l[:1]>=l[1:2]]+f(l[1:],l[0])
```
[Try it online!](https://tio.run/##bZDBasMwDIbvfgqRS2IqRu24PYS6L@Lp4NKFBhzHNGGl0HfPZI9BA7MQEv9n/8hKz@U2Rb2uvQ1@vFw9BEx23wXw8QrBdS7RyXJVdOaiOk2065vcEQa3J7nOtqoqZ1DjEZVGgwfOlgBewCorBxJOoWZlcxjrX4Itp6J30jIpaqZbgpkZNP/4ZVW4PEOOrWPRCmWH7eviaoi/IUQ/3eEBQ4T5Y05hWJr6M9ayE8CLGe3oU/P17QM@/uirlpJhug9xAV6MtHZEvlv69Qc "Python 2 – Try It Online")
Alt 57:
```
f=lambda l,p=0:l and l[l<[max(p,*l[:2])]:1]+f(l[1:],l[0])
```
[Answer]
## Mathematica 22 Bytes
```
Pick[#,MaxDetect@#,1]&
```
[Answer]
## Haskell, ~~50~~ ~~49~~ 42 bytes
```
f l=[j|i:j:k:_<-scanr(:)[0]$0:l,k<=j,i<=j]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hxzY6qybTKssq2yreRrc4OTGvSMNKM9ogVsXAKkcn28Y2SycTSMT@z03MzFOwVUjJ51JQUCgoyswrUVBRSFOINtEx0jHTMTTSMdExBWLjWAU0BYZABcZAbBiLJgEWBEnG/gcA "Haskell – Try It Online")
`scanr(:)[0]` makes a list of the tails of `(0:l)`, each with a final `0`, e.g. for `l = [4,3,3,4]`: `[[0,4,3,3,4,0],[4,3,3,4,0],[3,3,4,0],[3,4,0],[4,0],[0]]` which is pattern matched agains `i:j:k:_` to extract all lists with at least 3 elements which are named `i`, `j`, and `k`. Keep `j` if its >= `i` and `j`.
Edit: Ørjan Johansen saved 7 bytes. Thanks!
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~13 12~~ 11 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
0;;0»3\f"⁸Ẏ
```
A monadic link taking a list of positive integers and returning the filtered list containing only those which are greater than or equal to all their neighbours.
**[Try it online!](https://tio.run/##y0rNyan8/9/A2trg0G7jmDSlR407Hu7q@///f7SJjpGOmY6hkY6JjikQG8cCAA)**
---
Previous [12 byter](https://tio.run/##y0rNyan8/9/A2tPv6B5PO4OHO1r8fB/u7v7//3@0iY6RjpmOoZGOiY5pLAA):
```
0;INżI>0ḄNMị
```
Previous [13 byter](https://tio.run/##y0rNyan8/9/A2trg4c6Fxr6PmtYc6QYSRiEPd3f///8/2kTHSMdMx9BIx0THVMckFgA "Jelly – Try It Online"):
```
0;;0ṡ3M€ċ€2Tị
```
---
[Answer]
# Dyalog APL, ~~31~~ ~~30~~ ~~28~~ ~~22~~ 21bytes
```
{⍵/⍨(⌈/=2⌷⊢)¨3,/∊0⍵0}
```
[Try it online!](http://tryapl.org/?a=%7B%u2375/%u2368%28%u2308/%3D2%u2337%u22A2%29%A83%2C/%u220A0%u23750%7D4%2C2%2C6%2C12%2C4%2C5%2C4%2C3&run)
Explanation (I'm not good at explaining things):
```
0⍵0       - [0,input,0]   (it looks like a face!)
∊         - flatten
3,/       - split into overlapping sections of length 3.
(⌈/=2⌷⊢)¨ - Whether the middle element is the maximum (applied to every section)
⍵/⍨       - index
```
[Answer]
# [Haskell](https://www.haskell.org/), 40 bytes
```
p%(h:t)=[h|h>=p,[h+1]>t]++h%t
_%e=e
(0%)
```
[Try it online!](https://tio.run/##NY7LCsMgEEX3fsUsKiTooja2i4L5EZHiwjChSZA6y/679ZHCcO/MGeaBPr3DtuUc@YBPGo3FL84mSotCuZmcEMiJvXgwgS1muPIxU0iUwIC1WsJNwkOCKlaKe9PJSbCqtP5eUFNVwZl2XEGZ0NX7ih7qBFML7Rzb/XqUm/GzHgQX2H2EBdon@Qc "Haskell – Try It Online")
[Answer]
## JavaScript (ES6), 40 bytes
```
a=>a.filter((e,i)=>!(e<a[i-1]|e<a[i+1]))
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~84 75\*~~ 71 bytes
```
lambda l,k=[0]:[j for x,j in enumerate(l)if(k+l+k)[x+2]<=j>=(k+l+k)[x]]
```
[Try it online!](https://tio.run/##VYm9DsIgGAB3n@IbIXyDBXQw0hdBBowQ@SltCCb16ZGpicMNd7d923stonv16Nkuz5eFjEnps7npCH6tsGOEUMCVz@KqbY5kGjxJLLNE9c64uas4qyMY07caSiOeaIkcrzhxlHgZCEPp6ZhizGnwXyXK4f0H "Python 3 – Try It Online")
---
*\*@LeakyNun saved 9 bytes using a clever operator trick.*
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 15 bytes
```
2ị⁼Ṁ
0;;0ṡ3Ç€Tị
```
[Try it online!](https://tio.run/##y0rNyan8/9/o4e7uR417Hu5s4DKwtjZ4uHOh8eH2R01rQoDi////jzbRMdIx0zE00jHRMQVi41gFAA "Jelly – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~15~~ ~~14~~ 13 bytes
```
ü‹0¸«sĆÁü›+_Ï
```
[Try it online!](https://tio.run/##MzBNTDJM/f//8J5HDTsNDu04tLr4SNvhRhB3l3b84f7//6NNdIx0zHQMjXRMdEyB2DgWAA "05AB1E – Try It Online")
**Explanation**
```
ü‹             # pairwise comparison for less than
  0¸«          # append 0
     s         # swap input to top of stack
      Ć        # enclose, append the head of the list
       Á       # rotate right
        ü›     # pairwise comparison for greater than
          +    # add the two boolean lists
           _   # logical negate
            Ï  # keep only elements of input that are true in the resulting list
```
**Previous 15 byte solution**
```
¬s¤)˜Œ3ùεZQ1è}Ï
```
[Try it online!](https://tio.run/##MzBNTDJM/f//0JriQ0s0T885Osn48M5zW6MCDQ@vqD3c//9/tImOkY6ZjqGRjomOKRAbxwIA "05AB1E – Try It Online")
**Explanation**
```
¬                # get head of input
 s¤              # get tail of input
   )˜            # wrap stack in flattened list
                 # produces the input list with the first and last element duplicated
     Œ3ù         # push sublists of length 3
        ε        # apply transformation on each triple
         ZQ      # ... check each element for equality to the max
          1è     # ... get the middle element
            }    # end transform
             Ï   # keep only elements of input that are true in the resulting list
```
[Answer]
# R, 44 bytes
```
pryr::f(x[(x>=c(0,x)&x>=x[-1])[1:sum(x|1)]])
```
which evaluates to the function:
```
function (x) 
x[(x >= c(0, x) & x >= x[-1])[1:sum(x | 1)]]
```
Compares `x` to `c(0,x)`, so with `x` shifted one position to the right. Also compares `x` to `x[-1]`, so one position shifted to the left. These both are `TRUE` if there is a maximum there. `&` to take the AND of these booleans. Because of the wrapping nature of R's vectors when they are not the same length, we have to truncate the result at the length of `x`, which is found by taking `sum(x|1)`. We then plug in the boolean vector, taking only the true indices of `x` and return that.
Note, because these logical operations are done with unequal length vectors, R will complain. A lot. But the correct output will be there amidst the warnings:
```
> pryr::f(x[(x>=c(0,x)&x>=x[-1])[1:sum(x|1)]])(c(4,2,6,12,4,5,4,3))
[1]  4 12  5
Warning messages:
1: In x >= c(0, x) :
  longer object length is not a multiple of shorter object length
2: In x >= x[-1] :
  longer object length is not a multiple of shorter object length
3: In x >= c(0, x) & x >= x[-1] :
  longer object length is not a multiple of shorter object length
```
[Answer]
# [R](https://www.r-project.org/), 42 bytes
```
function(x)x[c(d<-diff(x),0)<=0&c(0,d)>=0]
```
[Try it online!](https://tio.run/##RYxLCoQwEET3cxDphhLy6XFlvIi4SmhwoyAK3j6TDEEpCt4rmj6yhqzXFs913@jme46Uxj6tqsVgeAymi2SQeApmyUqRBA4DrIPgW@qZP3W2cC/Bl9rmf65bc4E0qj9q7OO@RJ67ZvkH "R – Try It Online")
2 bytes shorter than [JAD's solution](https://codegolf.stackexchange.com/a/132547/86301). `diff` computes successive differences; then keep only the entries greater than both neighbours.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 20 bytes
```
-.b*YqYeSN.:++0Q03Q0
```
To be golfed...
[Test suite.](http://pyth.herokuapp.com/?code=-.b%2aYqYeSN.%3A%2B%2B0Q03Q0&test_suite=1&test_suite_input=%5B4%2C2%2C6%2C12%2C4%2C5%2C4%2C3%5D%0A%5B1%2C2%5D%0A%5B1%2C2%2C3%2C2%2C1%5D%0A%5B3%2C2%2C1%2C2%2C3%5D%0A%5B4%2C4%5D%0A%5B2%2C4%2C4%2C4%2C1%5D%0A%5B2%2C3%2C3%2C4%5D&debug=0)
[Answer]
# [R](https://www.r-project.org/), 68 bytes
```
function(a)a[a==sapply(1:length(a),function(i)max(c(0,a,0)[i+0:2]))]
```
[Try it online!](https://tio.run/##K/qfpmCj@z@tNC@5JDM/TyNRMzE60da2OLGgIKdSw9AqJzUvvSQDKKwDV5KpmZtYoZGsYaCTqGOgGZ2pbWBlFKupGfs/DShoomOkY6ZjrmOiYwrExpqa/wE "R – Try It Online")
[Answer]
# [PHP](https://php.net/), 67 bytes
```
for(;$g=$argv[+$i];$l=$g)$g<$l|$g<$argv[++$i]?:$r[]=$g;print_r($r);
```
[Try it online!](https://tio.run/##K8go@G9jX5BRwKWSWJReZhutpKuko2QCxEZAbAbEhkZQAVMobawUa/0/Lb9Iw1ol3RasK1pbJTPWWiXHViVdUyXdRiWnBkRCZEBS9lYqRdGxQFnrgqLMvJL4Ig2VIk3r//8B "PHP – Try It Online")
[Answer]
# [Retina](https://github.com/m-ender/retina), 51 bytes
```
\d+
$*
^
 
M!`(?<=(^|1+) )(\1\d*)\b(?! 1\2)
^¶
%`1
```
[**Try it online**](https://tio.run/##K0otycxL/P8/JkWbS0WLK45LgctXMUHD3sZWI67GUFtTQVMjxjAmRUszJknDXlHBMMZIkyvu0DYuLtUEw///TRSMFMwUDI0UTBRMgdgYAA)
[Answer]
# q, 39 bytes
```
{x where x = -1 _ next 3 mmax x,last x}
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 10 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
úâH◄(☼bM•Å
```
[Run and debug it](https://staxlang.xyz/#p=a3834811280f624d078f&i=[4,2,6,12,4,5,4,3]%0A[1,2]%0A[1,2,3,2,1]%0A[3,2,1,2,3]%0A[4,4]%0A[2,4,4,4,1]%0A[2,3,3,4]%0A[4,3,3,4]&a=1&m=2)
It produces output as newline separated values on standard output.
Unpacked, ungolfed, and commented, it looks like this.
```
f       filter each value in input using the rest of the program; implicitly printing kept values
  x0|S  input pre- and post-pended with zero
  3B    split into batches of 3
  i@    get the i-th batch, where i is the iteration index
  |M=   is the current value equal to the max from the batch?
```
[Run this one](https://staxlang.xyz/#c=f+++++%09filter+each+value+in+input+using+the+rest+of+the+program%3B+implicitly+printing+kept+values%0A++x0%7CS%09input+pre-+and+post-pended+with+zero%0A++3B++%09split+into+batches+of+3%0A++i%40++%09get+the+i-th+batch,+where+i+is+the+iteration+index%0A++%7CM%3D+%09is+the+current+value+equal+to+the+max+from+the+batch%3F&i=[4,2,6,12,4,5,4,3]%0A[1,2]%0A[1,2,3,2,1]%0A[3,2,1,2,3]%0A[4,4]%0A[2,4,4,4,1]%0A[2,3,3,4]%0A[4,3,3,4]&m=2)
**Updated:** Just found a 9-byte solution. Will update explanation later:
# [Stax](https://github.com/tomtheisen/stax), 9 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
▀▓ûa¥╓╧↨⌐
```
[Run and debug it](https://staxlang.xyz/#p=dfb296619dd6cf17a9&i=[4,2,6,12,4,5,4,3]%0A[1,2]%0A[1,2,3,2,1]%0A[3,2,1,2,3]%0A[4,4]%0A[2,4,4,4,1]%0A[2,3,3,4]%0A[4,3,3,4]&a=1&m=2)
[Answer]
# [Perl 5](https://www.perl.org/) `-a`, 37 bytes
```
map{$_>=$F[++$i]&$_>=$p&&say;$p=$_}@F
```
[Try it online!](https://tio.run/##K0gtyjH9/z83saBaJd7OVsUtWltbJTNWDcwpUFMrTqy0VimwVYmvdXD7/99EwUjBTMHQSMFEwRSIjf/lF5Rk5ucV/9f1NdUzMDT4r5sIAA "Perl 5 – Try It Online")
]