# Increment an Array

Given a nonempty array of positive integers, "increment" it once as follows:

• If all the array elements are equal, append a 1 to the end of the array. For example:

[1] -> [1, 1]
[2] -> [2, 1]
[1, 1] -> [1, 1, 1]
[3, 3, 3, 3, 3] -> [3, 3, 3, 3, 3, 1]

• Else, increment the first element in the array that is the array's minimum value. For example:

[1, 2] -> [2, 2]
[2, 1] -> [2, 2]
[3, 1, 1] -> [3, 2, 1] -> [3, 2, 2] -> [3, 3, 2] -> [3, 3, 3]
[3, 4, 9, 3] -> [4, 4, 9, 3] -> [4, 4, 9, 4] -> [5, 4, 9, 4] -> [5, 5, 9, 4] -> ...


(Each -> represents one increment, which is all your program needs to do.)

Output the resulting incremented array.

The shortest code in bytes wins.

• Does 0 count as positive integer – Downgoat Nov 28 '16 at 23:14
• @Downgoat 0 is not ever positive on PPCG. If 0 was allowed, the term would be "non-negative" – ETHproductions Nov 28 '16 at 23:23

# Pyth, 16 bytes

?tl{QXxQhSQQ1+Q1


A program that takes input of a list and prints the result.

Test suite

How it works

?tl{QXxQhSQQ1+Q1  Program. Input: Q
?                 If:
l                The length
{Q              of Q deduplicated
t                 - 1
is non-zero:
X     Q1       Increment in Q at index:
xQ             Index in Q of
h            the first element
SQ          of Q sorted (minimum)
else:
+     Append
1   1
Q    to Q
Implicitly print


f z|and$(==)<$>z<*>z=z++[1]|1>0=z#minimum z where(x:z)#m|x==m=x+1:z;(x:z)#m|1>0=x:z#m;[]#_=[]

Ungolfed:

incrementArray :: [Int] -> [Int]
incrementArray xs | and [x == y | x <- xs, y <- xs] = xs ++ [1]
| otherwise = g xs (minimum xs)
where g (x:xs) m | x == m = (x + 1):xs
g (x:xs) m | otherwise = x:g xs m
g [] _ = []


Initial attempt, will try to come up with something more sophisticated later.

• Why not make a separate function instead of using where? – Michael Klein Nov 29 '16 at 3:45

# Wonder, 44 bytes

@[dp1unq#0?:=#[:0(iO f\min#0)#0+1f]#0?++#0 1


This is not what I had in mind when I made this language... It's literally worse than Perl in terms of readability!

Usage:

(@[dp1unq#0?:=#[:0(iO f\min#0)#0+1f]#0?++#0 1])[3 4 9 3]


# Explanation

@[
dp 1 unq #0
? set #[
get 0 (iO f\ min #0) #0
+ 1 f
] #0
? con #0 1
]


Basically checks if dropping 1 item from the unique subset of the argument makes the list empty. If not, then we increment the minimum of the array. Otherwise, we simply concatenate 1 to the argument.

## Kotlin, 75 bytes

fun a(s:MutableList<Int>){if(s.toSet().size<2)s+=0;s[s.indexOf(s.min())]++}


Modifies the function argument.

Damn you strong typing! :MutableList<Int> accounts for 17 bytes alone. I don't think there is a solution where the type can be inferred, unfortunately.

# jq, 44 characters

if unique[1]then.[index(min)]+=1else.+[1]end


Sample run (-c (--compact-output) option used only here for readability):

bash-4.3$jq -c 'if unique[1]then.[index(min)]+=1else.+[1]end' <<< '[3,1,1]' [3,2,1] bash-4.3$ jq -c 'if unique[1]then.[index(min)]+=1else.+[1]end' <<< '[3,2,1]'
[3,2,2]

bash-4.3$jq -c 'if unique[1]then.[index(min)]+=1else.+[1]end' <<< '[3,2,2]' [3,3,2] bash-4.3$ jq -c 'if unique[1]then.[index(min)]+=1else.+[1]end' <<< '[3,3,2]'
[3,3,3]

bash-4.3$jq -c 'if unique[1]then.[index(min)]+=1else.+[1]end' <<< '[3,3,3]' [3,3,3,1]  On-line test: # PHP, 79 bytes function i($a){max($a)>($n=min($a))?$a[array_search($n,$a)]++:$a[]=1;return$a;}


I think this needs no explanation.

## Groovy - 45 bytes

A groovy closure which accepts a list of Integer as input - please note this modifies the input List.

{o->(o-o[0])?o[o.indexOf(o.min())]+=1:o<<1;o}

Test cases can be seen here:

http://ideone.com/9vRd9e

Alternative of 56 bytes if the code needs to work with int[] or List<Integer>:

{o=it.toList();(o-o[0])?o[o.indexOf(o.min())]+=1:o<<1;o}

# MATLAB, 69 67 bytes

function a=f(a);if~range(a)a=[a 1];else[~,j]=min(a);a(j)=a(j)+1;end


range(a) is non-zero if not all elements are equal, and [~,j]=min(a) assigns the index of the minimum value in a to j, defaulting to the first value it finds. Input is a row vector. Saved 2 bytes by reversing the if/else logic to get rid of some semicolons (else[~,j] is legal, but elsea=[a 1] is not).

# Actually, 18 bytes

The algorithm used in this answer is largely based on Dennis' Jelly answer. Golfing suggestions welcome! Try it online!

;;M@u#q;m@í0@α1@q¥


Ungolfing

      Implicit input a.
;;    Duplicate a twice.
M     Get the maximum of a.
@u#   Increment all of a and convert back to a list.
q;    Append max(a) to the end of a_plus_one, and duplicate. Call it b.
m@í   Get index of the minimum of b. Call it min_index.
If all elements of a were equal, this is the maximum at the end.
Else, it's somewhere else in the array.
0@α   Push a list of min_index zeroes.
1@q   Append a 1 to the end. Call this array c.
This will increment at the desired index or otherwise append a 1.
¥     Pairwise add a and c to get our incremented array.
Implicit return.


proc I a {if [llength [set S [lsort -u $a]]]<2 {lappend a 1} {lset a [lsearch$a [set m [lindex $S 0]]] [incr m]}}  Try it online! # Perl 5-MList::Util=all,reduce -pa, 75 bytes (all{$_==$F[0]}@F)?push@F,1:$F[reduce{$F[$a]>$F[$b]?$b:$a}0..$#F]++;$_="@F"


Try it online!

# Julia, 46 bytes

!x=x[1:1]==x∪x ? [x;1] : (x[argmin(x)]+=1;x)


x∪x is union(x,x), which is equivalent to unique(x)

Try it online!