[Question]
  [
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.**
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~8~~ 7 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
‘;ṀỤḢṬ+
```
[Try it online!](http://jelly.tryitonline.net/#code=4oCYO-G5gOG7pOG4ouG5rCs&input=&args=WzMsIDMsIDMsIDMsIDNd) or [verify all test cases](http://jelly.tryitonline.net/#code=4oCYO-G5gOG7pOG4ouG5rCsKxbzDh-KCrEvigqzigqxq4oKs4oCcIC0-IOKAnVk&input=&args=WzFdLCBbMl0sIFsxLCAxXSwgWzMsIDMsIDMsIDMsIDNdLCBbMSwgMl0sIFsyLCAxXSwgWzMsIDEsIDFdLCBbMywgMiwgMV0sIFszLCAyLCAyXSwgWzMsIDMsIDJdLCBbMywgNCwgOSwgM10sIFs0LCA0LCA5LCAzXSwgWzQsIDQsIDksIDRdLCBbNSwgNCwgOSwgNF0).
### How it works
```
‘;ṀỤḢṬ+  Main link. Argument: A
‘        Increment all elements of A.
  Ṁ      Yield the maximum of A.
 ;       Concatenate both results. Note that the appended maximum will be the 
         minimum of the resulting array if and only if all elements of A are equal.
   Ụ     Grade up; yield the indices of the resulting array, sorted by their
         corresponding values in that array.
    Ḣ    Head; extract the first index, which is the index of the first occurrence
         of the minimum. For an array of equal elements, this will be the index
         of the appended maximum.
     Ṭ   Untruth; for index i, yield an array of i-1 zeroes, followed by a 1.
      +  Add this array to A, incrementing the minimum or appending a 1.
```
[Answer]
# Python 3, ~~62~~ ~~53~~ ~~51~~ 50 bytes
Function which modifies the list passed to it ([allowed by meta](http://meta.codegolf.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/4942#4942)). 
```
def F(a):a+=1//len({*a})*[0];a[a.index(min(a))]+=1
```
[**Try on repl.it!**](https://repl.it/E9oy/2)
-9 bytes thanks to Lynn for spotting that, because the array will be of positive integers, I can append '0' to the end of the array and have that incremented.
Special thanks to mbomb007 for golfing `len(set(a))` to `len({*a})`, and Dennis for the floordiv trick!
[Answer]
# Mathematica, ~~70~~ ~~57~~ 55 bytes
*Virtually all of the improvement is due to Martin Ender, who kicks my ass at pattern matching approaches! Also JHM came up with essentially the same solution at essentially the same time.* (byte count uses ASCII encoding)
```
±{p:x_ ..}:={p,1};±{x___,y_,z___}/;y≤x~Min~z:={x,y+1,z}
```
Defines a function `±` taking one list argument. If that list argument contains some number of copies of the same element (detected by `x_..` and named `p`), then output the list with a `1` appended. Otherwise, if that list argument has a special element `y` (with `x` being the zero or more elements before `y`, and `z` being the zero or more elements after `y`) which is at most the minimum of the other elements, then output the list with that `y` incremented. Any instance of the minimum element of the list will be matched by `y`, but fortunately Mathematica chooses the first one to act upon.
[Answer]
# JavaScript (ES6), 61 bytes
```
a=>new Set(a).size>1?++a[a.indexOf(Math.min(...a))]:a.push(1)
```
Outputs by [modifying its argument](https://codegolf.meta.stackexchange.com/a/4942/42545). I can't find a way to determine whether an array has only one unique item in less that 17 bytes, but suggestions are welcome.
### Test snippet
```
f=a=>new Set(a).size>1?++a[a.indexOf(Math.min(...a))]:a.push(1)
g=a=>0 in a?console.log("Input:",`[${a}]`,"Output:",`[${f(a),a}]`):console.log("Invalid input")
g([1])
g([2])
g([1,1])
g([1,2,2,3])
g([2,2,2,3])
g([3,2,2,3])
g([3,3,2,3])
g([3,3,3,3])
g([3,3,3,3,1])
```
```
<input id=I value="1,2,2,3"><button  onclick="g(I.value.match(/\d+/g)||[])">Run</button>
```
### Other attempts
Here are a few alternate ways of deciding whether the array has more than one unique input:
```
a=>a.some(x=>x-a[0])?++a[a.indexOf(Math.min(...a))]:a.push(1)
a=>a.some(x=>x-m,m=Math.min(...a))?++a[a.indexOf(m)]:a.push(1)
```
Both of the `some`s can be replaced with `find` as well. `.sort` would be shorter for finding the minimum, if the default sort wasn't lexicographical (why, JS, why?):
```
a=>new Set(a).size>1?++a[a.indexOf(a.sort()[0])]:a.push(1)
// Instead we have to do:
a=>new Set(a).size>1?++a[a.indexOf(a.sort((x,y)=>x-y)[0])]:a.push(1)
```
I tried recursion to find the minimum, but it turned out way longer:
```
f=(a,n=1,q=a.indexOf(n))=>~q?a.some(x=>x-n)?++a[q]:a.push(1):f(a,n+1)
```
And here's a string-based solution which seemed like a good idea at first: (input is given in array format in a string, e.g. `"[1,2,3]"`)
```
a=>a.replace(m=/(\d+),(?!\1)/.test(a)?Math.min(...eval(a)):']',+m+1||",1]")
```
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), ~~21~~ ~~20~~ 16 bytes
Saved 4 bytes thanks to *Adnan*.
```
DÙgi0¸«}ÐWksgÝQ+
```
[Try it online!](http://05ab1e.tryitonline.net/#code=RMOZZ2kwwrjCq33DkFdrc2fDnVEr&input=WzQsMyw5LDNd)
**Explanation**
```
                      # input = [3,2,1] used as example
D                     # duplicate input
 Ùgi                  # if all elements are equal
    0¸«}              # append 0
        Ð             # triplicate list
                      # STACK: [3,2,1], [3,2,1], [3,2,1]
         Wk           # index of minimum element
                      # STACK: [3,2,1], [3,2,1], 2
           s          # swap top 2 elements of stack
                      # STACK: [3,2,1], 2, [3,2,1]
            g         # length of list
                      # STACK: [3,2,1], 2, 3
             Ý        # range [0 ... length]
                      # STACK: [3,2,1], 2, [0,1,2,3]
              Q       # equal
                      # STACK: [3,2,1], [0,0,1,0]
               +      # add
                      # OUTPUT: [3,2,2]
```
[Answer]
# C++14, ~~178 176 174 155 142~~ 135 bytes
# submission
```
#include<list>
#include<algorithm>
[](auto&l){auto e=end(l),b=begin(l);l.size()^count(b,e,*b)?++*min_element(b,e):(l.push_back(1),0);};
```
# invocation
```
std::list<int> s = {4, 4, 9, 4};
//invoke like this
auto i = [](auto&l){auto e=end(l),b=begin(l);l.size()^count(b,e,*b)?++*min_element(b,e):(l.push_back(1),0);};
i(s);
//or like that
[](auto&l){auto e=end(l),b=begin(l);l.size()^count(b,e,*b)?++*min_element(b,e):(l.push_back(1),0);}(s);
```
## ungolfed
```
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
void i(list<int>& l) {
    auto e = l.end(), b = l.begin();
    
    if (l.size() == count(b, e, l.front())) {
        l.push_back(1);
    } else {
        ++*min_element(b, e);
    }
}
int main() {
    list<int> s = {4, 4, 9, 4};
    //invoke like this
    i(s);
    
    for (auto o:s)
        std::cout << o << ' ';
    std::cout << std::endl;
}
```
This is my first time playing golf, help is appreciated.
EDIT: forgot to mention you have to compile it with at least ~~`-std=c++11`~~ `-std=c++14`
EDIT2: I realized i can leave out the space in the includes `#include <list>`
EDIT3: saved two more bytes by replacing `l.begin()` by `begin(l)`
EDIT4: saved another 19(!) bytes thanks to @Quentin (see his comment)
EDIT5: Quentin shaved off 13 more bytes, thanks!
EDIT6: as TuukkaX pointed out, unnamed lambdas/functions suffice so i removed the `auto i=` in the bytecount
[Answer]
## Scratch, ~~25~~ 34 blocks + ~~7~~ 6 bytes
[![Program](https://i.stack.imgur.com/vgMtN.png)](https://i.stack.imgur.com/vgMtN.png)
Takes input as a predefined array of integers. Note that arrays are 1-indexed in Scratch.
In Python, this would look like: (Note that unlike Scratch, Python is 0-indexed)
```
lowval = 0
hival = 0
n = 1
for i in range(len(input)):
    if(input[i] < input[lowval]):
        lowval = i
    if(input[i] > input[hival]):
        hival = i
    # No increment statement needed because python.
if(lowval == hival):
    input.append(1)
else:
    input[lowval] += 1
print(input)
```
[Answer]
## J, ~~25~~ 22 bytes
```
(+~:*[=<./)@,0#~1=#@~.
```
Evaluates to an anonymous verb.
[Try It Online!](https://tio.run/nexus/j#@5@mYGuloKFdZ6UVbWujp6/poGOgXGdoq@xQp/c/NTkjXyFNQUfBkAvONIIxDRGixjCIkIMrM0JWhqLHRMESocMEG9cExjWFcP8DAA "J – TIO Nexus")
## Explanation
```
(+~:*[=<./)@,0#~1=#@~.  Input is y.
                  #@    Is the length of
                    ~.   deduplicated y
                1=       equal to 1?
            ,0#~        Append that many 0s to y (one or none).
(         )@            Call the result z and apply this verb to it:
      =                  take the bit array of equality
     [                   between z
       <./               and its minimum element,
    *                    multiply that element-wise by
  ~:                     the bit array of first occurrences in z
 +                       and add the result to z.
```
[Answer]
# C#, ~~123~~ ~~121~~ ~~120~~ ~~79~~ 77 bytes
```
using System.Linq;l=>{if(l.All(o=>o==l[0]))l.Add(0);l[l.IndexOf(l.Min())]++;}
```
Modifies the argument passed to the function.
Thanks to [Cyoce](https://codegolf.stackexchange.com/users/41042/cyoce) for saving 3 bytes! -> `!Any` to `All`, `+=1` to `++`.
Thanks to [TheLethalCoder](https://codegolf.stackexchange.com/users/38550/thelethalcoder) for saving a whopping 43 bytes! -> Removed method signature code.
Removed parenthesis around the parameter list.
[Answer]
# [R](https://www.r-project.org/), ~~72 66~~ 65 bytes
```
"if"(any((x=scan())-x[1]),"[<-"(x,u<-which.min(x),1+x[u]),c(x,1))
```
[Try it online!](https://tio.run/##K/r/XykzTUkjMa9SQ6PCtjg5MU9DU1O3ItowVlNHKdpGV0mjQqfURrc8IzM5Qy83M0@jQlPHULsiuhQonwyUM9TU/G@kYKRg/B8A "R – Try It Online")
Increment is done using `which.min` which returns the first match. `"[<-"` allows to replace the value and returns the modified vector in one function call.
-7 bytes thanks to Giuseppe!
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 48 bytes
```
f←{1≥≢∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24Rqw0edSx91LnrUsepR71YrINYxfNTdYqjtoPGoq/lRbzdQRPNR1yIgVQvUoWDIBcRQ0ghIGikYQ9gKxgomYD6MZQyWAZIKhgA "APL (Dyalog Unicode) – Try It Online")
Alternate versions of `1+@(⊃⍋⍵)⊢⍵`: `⍵+(⍳≢⍵)=⊃⍋⍵`, `⊢+⍳∘≢=⊃∘⍋`. -2 thanks to @Adam and @Razetime.
Explanation:
```
{1≥≢∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}
 1≥≢∪⍵                   ⍝ if all elements are identical
       ⍵,1               ⍝ then yield ⍵ with 1 appended to the end.
          ⋄              ⍝ ... else,
              (⊃⍋⍵)      ⍝ index of the smallest value
           1+@     ⊢⍵    ⍝ increment the value from ⍵
```
[Answer]
# [MATL](http://github.com/lmendo/MATL), 16 bytes
```
t&=?1h}t2#X<wQw(
```
[Try it online!](http://matl.tryitonline.net/#code=dCY9PzFofXQyI1g8d1F3KA&input=WzMsMSwxXQ) Or [verify all test cases](http://matl.tryitonline.net/#code=IkBnCnQmPT8xaH10MiNYPHdRdyg&input=e1sxXSBbMl0gWzEsIDFdIFszLCAzLCAzLCAzLCAzXSBbMSwyXSBbMiwxXSBbMywxLDFdIFszLCA0LCA5LCAzXX0)
### How it works
```
t         % Take input implicitly. Duplicate
&=        % Matrix of all pairwise equality comparisons
?         % If all comparisons were true
  1h      %   Append 1 to the original copy ofthe array
}         % Else
  t       %   Duplicate array
  2#X<    %   Push minimum and index of its first occurrence
  wQw     %   Swap, increment, swap (adds 1 to the minimum)
  (       %   Assign the incremented minimum to that position
          % End if implicitly. Display implicitly
```
[Answer]
# [Perl 6](https://perl6.org), 46 bytes
```
{.[[==]($_)??.elems!!.first(*==.min,:k)]++;$_}
```
(modifies the input Array, and returns it)
## Expanded:
```
{     # bare block lambda with implicit parameter ｢$_｣
  .[      # use the following as an index into the array
      [==]( $_ )    # reduce the array with ｢&infix:<==>｣
    ??              # if they are equal
      .elems        # the value past the end ( ｢.end+1｣ would also work )
    !!              # else
      .first(       # find the first value
        * == .min,  # where the element is equal to the minimum
        :k          # return the key rather than the value
      )
  ]++;              # increment it ( auto vivifies if it doesn't exist )
  $_                # return the modified array
}
```
## 
[Answer]
# Mathematica, 56 bytes
```
±{b:a_ ..}:={b,1};±a_:=a/.{p___,b:Min@a,q___}:>{p,b+1,q}
```
Uses named function `±`. Uses ISO8859-1 encoding
**Alternative solutions (58 bytes)**
```
±{b:a_ ..}:={b,1};±{p___,b_,q___}/;b<=p~Min~q:={p,b+1,q}
(* @GregMartin and I both independently came up with this above solution *)
±{b:a_ ..}:={b,1};±a:{p___,b_,q___}/;b==Min@a:={p,b+1,q}
```
**Usage**
```
±{1, 1}
```
> 
> `{1, 1, 1}`
> 
> 
> 
```
±{3, 4, 5}
```
> 
> `{4, 4, 5}`
> 
> 
> 
[Answer]
## Haskell, ~~71 70~~ 62 bytes
```
f(a:b)|(x,y:z)<-span=<<(<).minimum$a:b++[0|all(a==)b]=x++y+1‌​:z
```
@Zgarb saved 8 bytes, thanks!
When I started I hoped for some elegant tying-the-knot trickery, but @Zgarb's way is just as amazing.
[Answer]
# [AWK](https://www.gnu.org/software/gawk/manual/gawk.html), 63 bytes
```
a=b=1{for(;$++a;d=$b==$a&&c==$a?a+1:b)b=(c=$b)>$a?a:b;$d++}++NF
```
[Try it online!](https://tio.run/##SyzP/v8/0TbJ1rA6Lb9Iw1pFWzvROsVWJcnWViVRTS0ZRNknahtaJWkm2WokAyU07UAiVknWKina2rXa2n5u//9bKpgAoTEA "AWK – Try It Online")
The code scans all the numbers presented on the input line and sets variable, `d`, to the position of the smallest number, or to the number of fields plus one if all values are equal. Then it increments the number at that position and prints everything. As a result it either adds a new field with a value of 1 or increments the smallest number in the given set of numbers.
The first condition/test is always true, and just does some initialization.
```
a=b=1
```
The code block is a `for` loop, which applies one statement to each number from the input line.
```
for(;$++a;d=$b==$a&&c==$a?a+1:b)b=(c=$b)>$a?a:b
```
The check to continue iterating just increments the `a` "current field" variable and stops when it's gets a 0 (by running off the end of the list).
```
    $++a
```
The body of the loop updates `b` such that it's always pointing to the position of the smallest number in the input. The code also has to save the previous position of the smallest number, stored in `c`, to deal with cases where the minimum is the last number on the line.
```
    b=(c=$b)>$a?a:b
```
The "end of loop" statement sets variable `d` to the current field "plus one" if the current value matches the current minimum as well as the previous minimum, or to the position of the current minimum otherwise.
```
    d=$b==$a&&c==$a?a+1:b
```
One the loop completes, the code increments whichever field `d` points to. If all the fields are the same, it will increment an uninitialized field, which in AWK will default to 0. Otherwise it will increment the first field with the smallest value.
```
$d++
```
The second condition/test just increments the number of fields in the input line, which will always evaluate to true. If all the fields were the same numeric value, that change in `NF` will cause a `1` to be appended to the list. And if it changed one of the presented fields the additional field printed as a result of incrementing `NF` will be an empty string.
```
++NF
```
[Answer]
# Jelly, 9 bytes
```
;1µ‘i¦E?Ṃ
```
Thanks to Dennis for the -2 bytes.
Body must be at least 30 characters; you entered ... .
[Answer]
**Mathematica, 53 bytes 57 bytes 59 bytes**
```
If[Equal@@#,#~Join~{1},x=#;x[[#~FirstPosition~Min@#]]++;x]&
```
[Answer]
# Java 8, 85 + 38 = 123 bytes
Void lambda taking a `List<Integer>` (output is mutated input). Byte count includes lambda and required import.
```
import static java.util.Collections.*;
```
```
l->{if(min(l)==max(l))l.add(0);int i=0,n;while((n=l.get(i))>min(l))i++;l.set(i,n+1);}
```
[Try It Online](https://tio.run/##hVFda8IwFH1uf8VFEJIZg@7jYastDJ8G25OPw4esRnddmpYk1Yn0t7vYdtSJ2yAk5NxzT87JXYuNGOaF1OvFxwGzIjcOrBMOU1j7Ei8dKj7NlZKpw1xbfhWFYVG@KU9IlbAWXgRq2IdBC7bNS9RCnUgsS10LeC1ty0yaSVd7RusmT9rJlTRJAqhTIzPp7wbigxome1ySDDVRNI4z8elPqrhYLMiIRqgdYDxiOtq@o5KE6FjxlXQEKU2aJoqDQaS4PYJMD8Y0qg6BT3HmeJPjAjIfhsycQb16nYMwK0uP2YLW3evco6mw0kIMWm7hFD/ygh8g7McVu4DeMOjWRcY1g8utdwxuGdz7vS5Xkd@WuQHSccTUG4SHxmdjP/jls/1fF6Vrs3ScR2PEriYm5Ay1XNhjhdSvUBr9pZ6X7j/52kCjcjJ3LtJUFo40Ak15trNOZtxDvPDzcep7UNznz4Qjvb6FYQJ922NNLtYaaFxWoV9VePgC)
This almost looks like Python with those method imports...
[Answer]
# [R](https://www.r-project.org/), 63 bytes
```
`if`(any((v=scan())-v[1]),{v[m]=v[m<-which.min(v)]+1;v},c(v,1))
```
[Try it online!](https://tio.run/##lY5BCoMwFET3OUWwm/@pFkQ3rc1JRDDEiIH6LRpTRDx7mlJKF8VFGZjFGxje6H1t2hokLQBOTEoSICauTCuMV1f2lQh1TR6dUd2pNwQOq2NauC1W4OIU0Wc8hLEDH3WjW0Oay4m3MylrBuJ24ONMYbtraXVzWy7MkBKfHSyu7MfACvufBNuYpEmEZ4hePmeeRVgE9MX5Hs73cf7G/gk "R – Try It Online")
Had a shot at this without peeking at the [previous R answer](https://codegolf.stackexchange.com/a/166251/95126) first.
Although the overall approaches are pretty similar, this pleasingly comes-out 2 bytes shorter.
Here we use a code block (enclosed in curly brackets `{}`) as an expression, which allows us to adjust a chosen element of the input vector `v`, and let the expression evaluate to the new value of `v`.  
Shorter, but less fancy than the [previous approach](https://codegolf.stackexchange.com/a/166251/95126) of abusing the indexing operator `[<-` as a function (which definitely deserves an upvote anyway).
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal) `aW`, ~~21~~ 18 bytes
*-3 bytes by using the register instead of variables.*
```
:≈[1J|:g£(nn¥=[›0£
```
[Try it Online With Equal Elements!](http://lyxal.pythonanywhere.com?flags=aW&code=%3A%E2%89%88%5B1J%7C%3Ag%C2%A3%28nn%C2%A5%3D%5B%E2%80%BA0%C2%A3&inputs=2%0A2%0A2%0A2%0A2%0A2&header=&footer=)
[Try it Online With Unequal Elements!](http://lyxal.pythonanywhere.com?flags=aW&code=%3A%E2%89%88%5B1J%7C%3Ag%C2%A3%28nn%C2%A5%3D%5B%E2%80%BA0%C2%A3&inputs=5%0A6%0A1%0A2%0A4%0A3&header=&footer=)
Explanation:
```
                    # Implicit input; a flag takes input as list
:                   # Duplicate input
 ≈[1J               # If all elements are equal: append 1
     |:g£           # Else: save minimum value to the register
         (          # For every element in the input:
          n         # Duplicate current element n
           n¥=[     # If current element n == register value:
               ›0£  # Increment current element and clear the register
                    # Implicit output; W flag outputs full stack
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 20 [bytes](https://github.com/abrudz/SBCS)
```
{⍵+<\⍵=⌊/⍵}⊢,0~⍬≡⊢~⊃
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///v/pR71Ztmxggafuop0sfSNc@6lqkY1D3qHfNo86FQHbdo67m/2mP2iY86u171DfV0x/IP7Te@FHbRCAvOMgZSIZ4eAb/1wDpU3/UNkld59CKtEMrNB/1rtJ41LnoUe8WoIzmo44ZQAMOrVC3VgeKLnjU1QYSVTe0NrI2VDC0NlaAQiDPyNoILAIRN1GwVDBWBwA "APL (Dyalog Unicode) – Try It Online")
First we append a zero if the input is unique, then increment the first minimum (which will be the trailing zero in the unique case, as the numbers in the input are positive)
`⊢~⊃` Input without all occurences of the first element.  
`⍬≡` Is this the empty vector? (are values in the input the same)  
`0~` 0 without the result of that (a single 0 if all values are the same, empty vector otherwise)  
`⊢,` Append this to the input
`{⍵+<\⍵=⌊/⍵}` Increment the first minimum.  
`⌊/⍵` Minimum value  
`⍵=` Boolean vector with 1's at all minima  
`<\` Less-than scan; only leaves the first 1  
`⍵+` Add this boolean vector to the previous vector
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), ~~20~~ 17 bytes
```
=,1|iᶠo{+₁ʰ}ʰtᵒhᵐ
```
[Try it online!](https://tio.run/##ATQAy/9icmFjaHlsb2cy//89LDF8aeG2oG97K@KCgcqwfcqwdOG1kmjhtZD//1syLDEsMywxXf9Y "Brachylog – Try It Online")
### Explanation
The first case is easy. The second is less so.
```
=,1|iᶠo{+₁ʰ}ʰtᵒhᵐ
=                  Assert that all elements are equal
 ,1                Append 1
   |               If that failed, try the following instead:
    iᶠ             Get a list of [value, index] pairs for every item in the array
      o            Sort: put smallest value first, and smallest index for equal values
       {   }ʰ      Apply this predicate to the first element of the resulting list:
        +₁ʰ          Add 1 to its first element (first occurrence of min value)
             tᵒ    Sort by the second element of each sublist (index)
               hᵐ  Head of each sublist (value)
```
Here's a worked example of the second branch:
```
               Input: [2,3,1,5,1]
iᶠ             [[2,0], [3,1], [1,2], [5,3], [1,4]]
  o            [[1,2], [1,4], [2,0], [3,1], [5,3]]
   {+₁ʰ}ʰ      [[2,2], [1,4], [2,0], [3,1], [5,3]]
         tᵒ    [[2,0], [3,1], [2,2], [5,3], [1,4]]
           hᵐ  [2,3,2,5,1]
```
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2), 11 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
ạ?1a:MçȮ⁺Ẹȧ
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728JKM0Ly_faMGCpaUlaboWWx7uWmhvmGjle3j5iXWPGnc93LXjxPIlxUnJxVAFC5aa6hjpGEM4AA)
#### Explanation
```
ạ?1a:MçȮ⁺Ẹȧ  # Implicit input
ạ            # Is it all equal?
 ?           # If it is:
  1a         #  Append a 1
    :        # Otherwise:
     M       #  Take the minimum
      ç  Ẹ   #  Parallely apply:
       Ȯ     #   Index of the minimum
        ⁺    #   Increment the minimum
          ȧ  #  Assign the new value
             # Implicit output
```
[Answer]
# [R](https://www.r-project.org), 45 bytes
```
\(x)`if`(sd(c(x,x)),x+!rank(x,,"f")-1,c(x,1))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72waMGiNNulpSVpuhY3dWM0KjQTMtMSNIpTNJI1KnQqNDV1KrQVixLzsoE8HaU0JU1dQx2QjKGmJlRTeZqGoSZXmoYRiEjWMARJQVlGUJYRXMwILmasA4ZwnomOJZxngoVnAuWZwngQ6xcsgNAA)
This would be 4 bytes longer as a full program, which is a fairer comparison to [earlier](https://codegolf.stackexchange.com/a/223872/78274) [answers](https://codegolf.stackexchange.com/a/166251/78274) that had been posted when backslash functions were not yet a thing, and full program was a golfier approach:
## [R](https://www.r-project.org), 49 bytes
```
`if`(sd(c(x<-scan(),x)),x+!rank(x,,"f")-1,c(x,1))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72waMHGNNsYjUyd4uTEPCBDM1OzemlpSZquxU3DhMy0BI3iFI1kjQobXZC8hqZOhSYQaysWJeZla1To6CilKWnqGuoAVegYampCNVbWcqVpGGoCCSMQkaxhCJKEsoygLCO4mBFczFgHDOE8Ex1LOM8EC88EyjOF8SAOWLAAQgMA)
[Answer]
# Ruby, 46 bytes
```
->a{a.uniq.size<2?a<<1:a[a.index(a.min)]+=1;a}
```
I feel like there's a better way to check if all elements are the same than `a.uniq.size<2`, but I'm too lazy to find it.
[Answer]
# Octave, ~~69~~ ~~67~~ 64 bytes
It was actually shorter to make this a complete named function than using both `input` and `disp`.
Saved 3 bytes thanks to Luis.
```
function x=f(x)
[a,b]=min(x);if any(x-a),x(b)++;else x=[x,1];end
```
Old answer, not using a function:
```
[a,b]=min(x=input(''));if any(x-a),x(b)++;else x(end+1)=1;end;disp(x)
```
[Answer]
## R, 97 bytes
```
if(all((a=scan())==a[1])){a=c(a,1)}else{while(!all(a==a[1])){a[which(a==min(a))][1]=min(a)+1}};a
```
Too bad that the synthax `x=+1` doesn't exist in R !
**Ungolfed :**
```
if(all((a=scan())==a[1]))
{
    a=c(a,1)
}
else
{
    while(!all(a==a[1]))
    {
        a[which(a==min(a))][1]=min(a)+1
    }
a
```
[Answer]
# TI-Basic, 53 bytes
```
If min(not(ΔList(Ans
Then
Ans->L1
cumSum(1 or Ans
min(Ans+ᴇ9(L1≠min(L1
L1(Ans)+1->L1(Ans
Else
augment(Ans,{1
End
```
[Answer]
# Matlab, ~~83~~, ~~77~~, 71 Bytes
```
function a=x(a)
if~nnz(a-a(1));a=[a,1];else[~,I]=min(a);a(I)=a(I)+1;end
```
I'm relatively new to code golf so please be kind! I tried to use anonymous functions but googling says you can't use if/else statements and matlab doesn't have ternary operators, so this is the best i felt I could do.
Edit: Corrected and shortened (twice!) thanks to stewie-griffin.
]