JavaScript (ES6), 61 bytes
a=>new Set(a).size>1?++a[a.indexOf(Math.min(...a))]:a.push(1)
Outputs by modifying its argument. 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]")