g | x | w | all
Bytes Lang Time Link
058Tcl171112T233847Zsergiol
044AWK250227T170510Zxrs
020Juby240314T002945ZJordan
003Vyxal 3240313T232733Zpacman25
077Swift240313T223749ZmacOSist
003Thunno 2 h230709T134543ZThe Thon
119C++ g++160825T041754Zceilingc
019Julia 0.6180104T181409Zgggg
087SNOBOL4 CSNOBOL4180104T180135ZGiuseppe
079C160924T022309ZNoSeatbe
019x86 opcode171229T184512Zl4m2
097Python171219T165112ZAlex Hul
110Python171115T001339ZBolce Bu
028Clojure171114T214333ZNikoNyrh
004Pyke160824T074307ZBlue
005APL Dyalog160824T081705ZAdá
066C++14161206T175723ZKarl Nap
065PHP160923T132639Zaross
052Processing160823T215954ZCody
058JavaScript ES6160923T193347ZSunny Pu
046JavaScript ES6160924T164238ZETHprodu
029Python160924T014443ZDennis
115Java160923T174056ZAMACB
031Python160823T213335Zacrolith
110dc160825T142558Zjuh
030Bash + coreutils160912T091103Zseshouma
026ARM Machine Code160825T181115ZIan Chew
055Scala160825T134037ZAmazingD
022zsh160825T031607Zizabera
034Python160824T015536ZTLW
035Octave160825T002442ZLuis Men
044POSIX Awk160824T225354ZZombo
044PHP160824T122359ZDexa
060C#160824T120435ZTheLetha
045dc160824T115006ZToby Spe
034Python 2160824T075103ZBlue
004MATL160824T012641ZLuis Men
034JavaScript ES6160824T003729ZNeil
038Factor160823T235727Zcat
013Perl 6160823T233630ZBrad Gil
027Haskell160823T231921ZMichael
009CJam160823T222432ZPurkkaKo
00405AB1E160823T214810ZEmigna
218BrainFlak160823T220421ZWheat Wi
012V160823T220553ZRiker
005Actually160823T213321Zuser4594
038BASH160823T215617ZRiley
023Ruby160823T213511Zanna328p
009Brachylog160823T214207ZLeaky Nu
017Retina160823T214206ZMartin E
036Haskell160823T213432ZThreeFx
003Jelly160823T213012ZLeaky Nu
020Octave160823T213105Zflawr
018Mathematica160823T212829ZMartin E

Tcl, 58 bytes

puts [lindex [set L [lsort -r {*}$argv]] 0]\ [lindex $L e]

Try it online!

AWK, 44 bytes

NR<2{b=$1}a<$1{a=$1}b>$1{b=$1}END{print b,a}

Attempt This Online!

J-uby, 20 bytes

:sort|-[:first,:pop]

Attempt This Online!

Vyxal 3, 3 bytes

SÞh

Try it Online!

sort, take the ends

Swift, 77 bytes

let f={(($0+[]).reduce(Int.max){$1>$0 ?$0:$1},$0.reduce(.min){$1<$0 ?$0:$1})}

The relatively obvious approach. I believe this is how the max() and min() methods are actually implemented for Sequence in the standard library.

Thunno 2 h, 3 bytes

ṠḲZ

Try it online!

Thunno 2, 4 bytes

Ṡçht

Try it online!

Explanation

ṠḲZ   # Implicit input
Ṡ     # Sort the list in ascending order
 Ḳ    # Bifurcate: duplicate and reverse
  Z   # Zip the two lists together
      # Implicit output of first item
Ṡçht  # Implicit input
Ṡ     # Sort the list
 ç    # Parallelly apply:
  h   #  First item
   t  #  Last item
      # Implicit output

C++ (g++), 148 133 131 122 119 bytes

#import<map>
#import<vector>
[](std::vector<int>v){int a=v[0],b=a;for(int i:v)i<a?a=i:i<b?:b=i;printf("%d %d\n",a,b);};

Try it online!

Julia 0.6, 19 bytes

x->sort(x)[[1,end]]

Try it online!

SNOBOL4 (CSNOBOL4), 87 bytes

	M =INPUT
	N =M
R	I =INPUT	:F(O)
	N =LT(I,N) I
	M =GT(I,M) I	:(R)
O	OUTPUT =N ' ' M
END

Try it online!

C, 83 81 79 bytes

m,M;f(a,s)int*a;{for(m=M=*a;s--;++a)*a<m?m=*a:*a>M?M=*a:0;pr‌​intf("%i %i",m,M);}

x86 opcode, 19 bytes

00: mov ebx, [esi]
02: mov edx, ebx
04: lodsd
05: cmp eax, ebx
07: cmovg ebx, eax
0A: cmp eax, edx
0C: cmovl edx, eax
0F: loop lp1
11: xchg eax, ebx
12: ret 

Input: ECX(length), ESI(array)

Output: EAX(max), EDX(min)

Removing xchg eax, ebx makes 18B but output is not usual reg

Python, 97 bytes

def k(s):
 d=s[0]
 if s[1:]:
  a,b=k(s[1:])
  return(a+d+abs(a-d))/2,(b+d-abs(b-d))/2
 return d,d

Only clever part is that in python [] is falsy. Only builtin used is abs.

It uses more builtins than the 110 byte python solution, and more bytes than the 26 byte solution using sort.

Python, 110 Bytes

Long, but it doesn't use sort.

def m(a):
 d=a[0]
 if len(a)==1:return d,d
 b,c=m(a[1:])
 if b>=d>=c:return b,c
 if d>b:return d,c
 return b,d

Output is a tuple of the form (max, min). Probably the least computationally efficient way to get the max/min of an array (other than bongo sorting it :P)

Clojure, 28 bytes

#((juxt first last)(sort %))

At least juxt is mildly interesting.

Pyke, 5 4 bytes

S'he

Try it here!

S    - sorted(input)
 'he - ^[0], ^[-1]

APL (Dyalog), 11 10 5 bytes

-5 thanks to Erik the Outgolfer.

According to the original poster, using a function that picks the largest of exactly two elements (in contrast to the largest of a whole array) [is] allowed. Therefore, the obvious solution is:

⌊/,⌈/

⌊/ minimum-reduction

, followed by

⌈/ maximum-reduction


More in the spirit of the challenge:

(⊃,⊢/)⍋⊃¨⊂

(

first

, followed by

⊢/ the last (lit. right-reduction)

) of

the indices of the elements in ascending order

⊃¨ each picks from

the entire argument

TryAPL online!

C++14, 66 bytes

As unnamed lambda returning via reference parameters. Accepts any random access container of integers.

[](int&a,int&b,auto&C){a=b=C[0];for(auto x:C)a=x<a?x:a,b=x>b?x:b;}

Ungolfed and usage:

#include<vector>
#include<iostream>

auto f=
[](int&a,int&b,auto&C){
  a=b=C[0];
  for(auto x:C)
    a=x<a?x:a,
    b=x>b?x:b;
}
;

int main(){
  std::vector<int> v{1,2,3,4,5,6,7,8,9,0,10,121,-12,100,100,100};
  int a;
  int b;
  f(a,b,v);
  std::cout << a << ", " << b << std::endl;
}

PHP, 66 65 bytes

Note: this doesn't use any sort builtin. Also, PHP 7 required for the null coalesce operator.

for(;n|$x=$argv[++$i];$x>$h&&$h=$x)$l=min($l??$x,$x);echo"$l,$h";

The max part is straightforward. The min part is a bit more elaborate. since in PHP null is less than 123, null needs to be handled. For this I'm using the null coalesce operator. If $l is null, use the current number ($x) instead, then take the smaller of the 2 numbers.

Run like this:

php -d error_reporting=30709 -r 'for(;n|$x=$argv[++$i];$x>$h&&$h=$x)$l=min($l??$x,$x);echo"$l,$h";' -- 0 15 2 3 7 18 -2 9 6 -5 3 8 9 -14;echo

37 bytes (with sort)

Being allowed to sort makes it trivial (windows-1252 encoding):

php -d error_reporting=30709 -r '$a=$argv;echo$a[sort($a)],~ß,end($a);' -- 0 15 2 3 7 18 -2 9 6 -5 3 8 9 -14;echo

Sort returns 1 when successful, and the list of arguments will start with - which will be sorted as the first item, so we need item 1 to yield min. Therefore we happen to be able to use the return value of sort as the index, saving 2 bytes.

Tweaks

Processing, 59 52 bytes

void m(int[]x){x=sort(x);print(x[0],x[x.length-1]);}

Processing doesn't actually let me read from stdin that I've been able to find, and I don't know if its internal Java compiler supports lambdas (and its been so long since I've had to write serious Java that I don't remember how).

JavaScript (ES6), 68 58 bytes (without sorting)

Oh my first try in Golf: without explicit sort (and without min max).
Thanks to @ETHproductions for golfing tips!!

a=>(b=[],a.map(n=>b[n]=0),[b.indexOf(0),b.lastIndexOf(0)])

Does not work with negative integers.

Tried @Luis Mendo 's approach but got it 2 bytes longer... Cost for supporting negative numbers lol?

a=>[a.find(n=>a.every(e=>e>=n)),a.find(n=>a.every(e=>e<=n))]

Could get shorter if order of output is not important:

a=>a.filter(n=>a.every(e=>e>=n)||a.every(e=>e<=n))

JavaScript (ES6), 46 bytes

I wanted to try an answer without sorting:

a=>a.map(i=>i<n?n=i:i>x?x=i:0,n=x=a[0])&&[n,x]

Bonus recursive version, 53 bytes:

f=([i,...a],n=i,x=i)=>i+.5?f(a,i<n?i:n,i>x?i:x):[n,x]

Python, 29 bytes

lambda s:s[s.sort():1]+s[-1:]

Test it on Ideone.

Java, 115 bytes

String f(int[]i){int t=i[0],a=t,b=t;for(int c=0;c<i.length;c++){a=i[c]<a?i[c]:a;b=i[c]>b?i[c]:b;}return""+a+" "+b;}

Ungolfed:

String f(int[] i) {
    int t=i[0], a=t, b=t; // assigns a and b to i[0]
    for (int c=0; c < i.length; c++) { // loop through the array
        a=(i[c]<a) ? i[c] : a;
        b=(i[c]>b) ? i[c] : b; // assignment with ternary operator
    }
    return ""+a+" "+b; // returns a string
}

My first ever code "golf" solution.

Python, 61 49 37 36 34 31 bytes

lambda s:s.sort()or[s[0],s[-1]]

-12 bytes thanks to RootTwo

Another -12 bytes thanks to chepner

-2 bytes thanks to johnLate

-3 bytes thanks to johnLate

dc, 110 bytes

?ddsMsmzdsAsa[z1-:az0<S]dsSx[>R]s?[la;asM]sR[lM]sQ[lQxla1-dsa;al?xla0<N]dsNxlAsa[<R]s?[la;asm]sR[lm]sQlNxlmlMf

Help me, dcers! You are my only hope!

Thanks to @seshoumara for finding that bug!

I'll add an explanation later. Here it is broken up a bit:

?dd sM sm
zd sA sa
[z 1- :a z0<S]dsSx
 [>R]s?
 [la;asM]sR
 [lM]sQ
[lQx la 1- dsa ;a l?x la0<N]dsNx
lA sa
 [<R]s?
 [la;a sm]sR
 [lm]sQ
lNx
lm lM f

Bash + coreutils, 30 bytes

tr \  \\n|sort -n|sed '$p;1!d'

The sed script prints, after the input is sorted, the first and last integers.

ARM Machine Code, 26 bytes

Hex dump (little endian):

6810 4601 f852 cb04 4560 bfc8 4660 4561 bfb8 4661 3b01 d8f5 4770

This is a function, with no system call or library dependence. The encoding is Thumb-2, a variable (2 or 4 byte) encoding for 32-bit ARM. As one might imagine, there's no easy way to just sort and pick the first and last elements here. Overall there's nothing really that fancy going on here, it's more or less the same as the reference implementation.

Ungolfed assembly (GNU syntax):

.syntax unified
.text
.global minmax
.thumb_func
minmax:
    @Input: @r0 and r1 are dummy parameters (they don't do anything)
    @r2 - Pointer to list of integers (int*)
    @r3 - Number of integers to sort (size_t)
    @Output:
    @Minimum of the list in r0 (int)
    @Maximum in r1 (int)
    ldr r0,[r2] @min=r2[0]
    mov r1,r0 @max=min
    loop:
        @ip is intra-procedure call register, a.k.a. r12
        ldr ip,[r2],#4 @ip=*r2++
        cmp r0,ip
        it gt @if (r0>ip)
        movgt r0,ip @r0=ip
        cmp r1,ip
        it lt @if (r1<ip)
        movlt r1,ip @r1=ip
        subs r3,r3,#1
        bhi loop @while (--r3>0)
    bx lr @Return

Tested on the Raspberry Pi 3; here's the testing script (C99, input through argv):

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//First 2 arguments are dummies.
uint64_t minmax(int,int,int* array,size_t size);

int main(int argc,char** argv) {
    int i;
    int array[argc-1];
    for (i=1;i<argc;i++) {
        array[i-1]=atoi(argv[i]);
    }
    uint64_t result = minmax(0,0,array,argc-1);
    printf("Minimum is %d, maximum is %d.\n",(unsigned)result,(unsigned)(result>>32));
}

Scala, 55 bytes

val s=args.map(_.toInt).sorted
print(s.head+" "+s.last)

To execute:

$ scala minmax.scala 1 2 3 4 5 6 7 8 9

zsh, 22 bytes

(){echo $1 $_} ${(n)@}

defines a lambda function that prints its first arg ($1) and the last argument to the previous command ($_), and passes it $@ after sorting it so the previous command becomes the invocation of that lambda


zsh, 21 bytes

this only works fine if there's more than 1 argument :(

<<<"${${(n)@}/ * / }"

sorts $@, makes it a string and replaces everything from the first space to the last one with a single space, then passes it as input to cat with <<<


usage:

$ ./minmax 23 342 21 10
10 342

Python, 35 34 bytes

lambda s:sorted(s+s[:1])[::len(s)]

Alternative version:

lambda s:sorted(s+s)[::len(s)*2-1]

Old version, 35 bytes.

lambda s:sorted(s+[s[0]])[::len(s)]

Fairly simple: take the input list, append the first element, sort it, then take the first and (length)th element of the resulting list. As the length of the input after appending an element is length + 1, this ends up taking the first and last element of said list, which are the minimum and maximum elements.

Octave, 35 bytes

@(x)[x(all(t=x<=x')) x(sum(t)==1)]

This is an anoynymous function. Try it at ideone.

The code avoids using sorting. Namely, it does all pairwise "less than or equal" comparisons between elements of the input. The minimum is the element for which all comparisons are true. The maximum is that for which only one comparison is true.

POSIX Awk, 44 bytes

awk '{for(;NF-1;NF--)if($1>$NF)$1=$NF}1' RS=

PHP, 44 bytes

function a($a){sort($a);echo $a[0].end($a);}

C#, 60 bytes

n=>{System.Array.Sort(n);return new[]{n[0],n[n.Length-1]};};

A naïve method at 93 bytes:

n=>{var r=new[]{n[0],n[0]};foreach(int i in n){if(i<r[0])r[0]=i;if(i>r[1])r[1]=i;}return r;};

dc, 45 bytes

[dsa]dsAx[dsz]dsZx[dla>Adlz<Zs.z0<f]dsfxlzlaf

This is a fairly straightforward equivalent of the C++ reference code:

#Test data
0 15 2 3 7 18 _2 9 6 _5 3 8 9 _14

# Uses two variables: a for min, and z for max

# Functions A and Z simply store a copy to a and z respectively.  Call
# them both, to initialize the variables.
[dsa]dsAx
[dsz]dsZx

# Function f is the main work - define and call it
[
    dla>A                       # copy into a if lower
    dlz<Z                       # copy into z if higher
    s.                          # discard value
    z0<f                        # recurse until end of input
]dsfx

# print the result
lzlaf

Python 2, 34 bytes

x=sorted(input());print x[0],x[-1]

MATL, 4 bytes

S5L)

Try it online!

Explanation

S    % Implicitly input the array. Sort
5L   % Push [1 0]. When used as a (modular, 1-based) index, this means "first and last"
)    % Apply as an indexing vector into the sorted array. Implicitly display

JavaScript (ES6), 34 bytes

a=>[a.sort((x,y)=>x-y)[0],a.pop()]

sort sorts in-place, so I can just refer to the [0] index for the lowest value and pop the highest value from the array, however it does a string sort by default so I have to pass a comparator.

Factor, 38 bytes

[ natural-sort [ first ] [ last ] bi ]

Sort the sequence and get its first and last elements.

Perl 6 13 bytes

*.sort[0,*-1]

Test:

my &min-max = *.sort[0,*-1];

say min-max 1;
# (1 1)
say min-max (0, 15, 2, 3, 7, 18, -2, 9, 6, -5, 3, 8, 9, -14)
# (-14 18)

Haskell, 27 bytes

f x=(`foldl1`x)<$>[min,max]

In Haskell, min and max give minimum and maximum of two arguments, not of a list. I couldn't tell whether this is disallowed (it seems that instead only minimum and maximum would be disallowed) so please let me know if they are and I'll promptly delete this answer.

CJam, 10 9 bytes

q~$_(p;W>

Try it online.

I'm really not good at CJam.

q~          e# eval input
  $         e# sort
   _        e# duplicate
    (       e# pop first
     p      e# print
      ;     e# remove array
       W>   e# get last element

05AB1E, 6 4 bytes

{Âø¬

Explanation

{     # sort list
 Â    # bifurcate
  ø   # zip
   ¬  # head

Try it online

Brain-Flak 220 218 bytes

(({}))([]){({}[()]<(([])<{({}[()]<([([({}<(({})<>)<>>)<><({}<>)>]{}<(())>)](<>)){({}())<>}{}({}<><{}{}>){{}<>(<({}<({}<>)<>>)<>({}<>)>)}{}({}<>)<>>)}{}<>{}>[()]){({}[()]<({}<>)<>>)}{}<>>)}{}({}<((())){{}{}([][()])}{}>)

Try It Online!

Explanation

First it doubles the top value (in cast the list is only one long)

(({}))

Then it uses my bubble sort algorithm:

([]){({}[()]<(([])<{({}[()]<([([({}<(({})<>)<>>)<><({}<>)>]{}<(())>)](<>)){({}())<>}{}({}<><{}{}>){{}<>(<({}<({}<>)<>>)<>({}<>)>)}{}({}<>)<>>)}{}<>{}>[()]){({}[()]<({}<>)<>>)}{}<>>)}{}

Then it picks up the top value of the stack (i.e. the min)

({}<...>)

Then it pops until the height of the stack is one:

((())){{}{}([][()])}{}

V, 12 bytes

:sor
ò2Gjkd

Try it online!

Credit to DJMcMayhem for this.

Actually, 5 bytes

S;F@N

Try it online!

Explanation:

S;F@N
S      sort
 ;     dupe
  F    first element
   @N  and last element

BASH 38

grep -o '[^ ]*'|sort -g|sed -n '1p;$p'

Ruby, 38 23 bytes

def m(v) puts v.sort![0];$><<v[-1];end

->v{[v.sort![0],v[-1]]}

Thanks @Jordan!

Brachylog, 9 bytes

oOtT,Oh:T

Try it online!

Retina, 17 bytes

O#`
$
¶$`
Ss`¶.+¶

Input and output are linefeed-separated.

Try it online!

Explanation

O#`

Sort lines by numerical value.

$
¶$`

Duplicate the entire input to make sure that the first and last element are distinct.

Ss`¶.+¶

Remove all intermediate lines, leaving only the minimum and maximum.

Haskell, 36 bytes

(\l->(head l,last l)).Data.List.sort

Would love to use &&& here but imports are so costly...

Jelly, 3 bytes

Ṣ.ị

Try it online!

Sort the array, and then takes the 0.5-th element.

Jelly uses 1-indexing, and floating points indexing means take its floor and its ceil.

So the 0.5-th element would give you the 0th element and the 1st element.

The 0th element is the last element.

Octave, 20 bytes

@(n)sort(n)([1,end])

This sorts the input vector and outputs the first and last value.

Mathematica, 18 bytes

Sort[#][[{1,-1}]]&

Sorts the array and extracts the first and last values.