Skip to main content
better golfed version
Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 26 bytes

≔⌈θη≔⌕θηζ≔⌈Φθ⁻κζζIEθ⎇⁼ιηζη 

Try it online! Link is to verbose version of code. Explanation:

≔⌈θη 

Get the maximum value of the input list.

≔⌕θηζ 

Get the index of its first occurrence.

≔⌈Φθ⁻κζζ 

Make a new list excluding that index, and take the maximum of that.

IEθ⎇⁼ιηζη 

Output that value for the maximum values in the list, and the maximum for the other values.

By comparison a fully golfed version would be 1919 9 bytes:

IEθ⎇⁼ι⌈θ⌈Φθ⁻μ⌕θ⌈θ⌈θIEθ⌈Φθ⁻μκ 

Try it online!Try it online! Link is to verbose version of code. Runs in O(n²) best case or O(n³) worst casetime. Explanation: For each element, creates a new list without that element, and takes the maximum of that list.

Charcoal, 26 bytes

≔⌈θη≔⌕θηζ≔⌈Φθ⁻κζζIEθ⎇⁼ιηζη 

Try it online! Link is to verbose version of code. Explanation:

≔⌈θη 

Get the maximum value of the input list.

≔⌕θηζ 

Get the index of its first occurrence.

≔⌈Φθ⁻κζζ 

Make a new list excluding that index, and take the maximum of that.

IEθ⎇⁼ιηζη 

Output that value for the maximum values in the list, and the maximum for the other values.

By comparison a fully golfed version would be 19 bytes:

IEθ⎇⁼ι⌈θ⌈Φθ⁻μ⌕θ⌈θ⌈θ 

Try it online! Link is to verbose version of code. Runs in O(n²) best case or O(n³) worst case.

Charcoal, 26 bytes

≔⌈θη≔⌕θηζ≔⌈Φθ⁻κζζIEθ⎇⁼ιηζη 

Try it online! Link is to verbose version of code. Explanation:

≔⌈θη 

Get the maximum value of the input list.

≔⌕θηζ 

Get the index of its first occurrence.

≔⌈Φθ⁻κζζ 

Make a new list excluding that index, and take the maximum of that.

IEθ⎇⁼ιηζη 

Output that value for the maximum values in the list, and the maximum for the other values.

By comparison a fully golfed version would be 19 9 bytes:

IEθ⌈Φθ⁻μκ 

Try it online! Link is to verbose version of code. Runs in O(n²) time. Explanation: For each element, creates a new list without that element, and takes the maximum of that list.

Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 26 bytes

≔⌈θη≔⌕θηζ≔⌈Φθ⁻κζζIEθ⎇⁼ιηζη 

Try it online! Link is to verbose version of code. Explanation:

≔⌈θη 

Get the maximum value of the input list.

≔⌕θηζ 

Get the index of its first occurrence.

≔⌈Φθ⁻κζζ 

Make a new list excluding that index, and take the maximum of that.

IEθ⎇⁼ιηζη 

Output that value for the maximum values in the list, and the maximum for the other values.

By comparison a fully golfed version would be 19 bytes:

IEθ⎇⁼ι⌈θ⌈Φθ⁻μ⌕θ⌈θ⌈θ 

Try it online! Link is to verbose version of code. Runs in O(n²) best case or O(n³) worst case.