## DEV Community is a community of 752,525 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Daily Challenge #184 - Form the Minimum

### Setup

Given an array of integers, implement a function that will return the lowest possible number that can be formed from these digits. You cannot use the same number more than once, even if it appears in the array multiple times.

### Examples

`minValue({1, 3, 1})` ==> return `13`

`minValue({5, 7, 5, 9, 7})` ==> return `579`

### Tests

`minValue({1, 2, 3, 4})`

`minValue({1, 1, 7, 0})`

`minValue({9, 2, 1, 4, 3, 9, 5})`

Good luck!

This challenge comes from MrZizoScream on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

## Discussion (15) Haskell. Also works for empty lists, returns 0.

``````import Data.List (nub, sort)
import Data.Maybe (fromMaybe)

minVal :: [Int] -> Int
minVal = fromMaybe 0 . readMaybe . concatMap show . sort . nub
`````` Valts Liepiņš • Edited on

Ruby:

``````def minValue arr
set = arr.uniq.sort
set[0..1] = set[0..1].rotate if set == 0 # Edge case for sets with 0
set.join.to_i
end
``````

Edit: Fixed the 0 edge case. Instead of adding 0 to the end of the number, put it in the 2. position. Simon Landry

Wouldn't rotating the array bring the biggest number at the front?

I think swapping the 1st and 2nd number would be ideal.

Also is having a leading zero really against the rules? Valts Liepiņš

Yeah, swapping seems to be better, would keep the large digits in the lesser valuable positions.

I interpreted the rules this way, because it didn't seem right to lose a digit in the resulting number. That would be like assuming input set `[1,2,0]` as equivelent to `[1,2]`. Michael Kohl • Edited on

Naive Ruby solution, no consideration for performance:

``````def min_value(arr)
uarr = arr.uniq
uarr.permutation(uarr.size)
.map { |a| a.map(&:to_s).join.to_i }
.min
end

min_value([1,3,1])
#=> 13
min_value([5, 7, 5, 9, 7])
#=> 579
min_value([1, 2, 3, 4])
#=> 1234
min_value([1, 1, 7, 0])
#=> 17 (017)
min_value([9, 2, 1, 4, 3, 9, 5])
#=> 123459
`````` Java

``````public String minValue(int[] ints) {
return Arrays.stream(ints)
//.filter(i -> i > 0) //does not include zeroes
.distinct()
.sorted()
.mapToObj(i -> String.valueOf(i))
.collect(Collectors.joining());
}
`````` Javascript

function getMinimum(arr){
let sorted = arr.sort();
let distinct = '';

sorted.forEach(function(element){
if(distinct.indexOf(element) < 0){
distinct = `\${distinct}\${element}`;
}
});

console.log(parseInt(distinct));
return parseInt(distinct);
} Vidit Sarkar • Edited on

Here is my C++ code

``````vector<int> minValueHelper(vector<int> v){
set<int> s;
bool presentZero = false;
for(int i : v){
if(i==0)
presentZero = true;
s.insert(i);
}
if(presentZero){
// because 0 cannot be in front of any number
// minValue({1, 1, 7, 0}) should not return 017 but should return 107
vector<int> v(s.begin(),s.end());

// if only zero is given in vector return zero
// else swaping
if(v.size() > 1)
swap(v,v);
return v;
}
return vector<int>(s.begin(),s.end());
}

int minValue(vector<int> v){
vector<int> minVector = minValueHelper(v);
string s = "";
for(int i : minVector)
s += to_string(i);
return stoi(s);
}

`````` My try to solve this challenge :)

In my solution I don't include 0 in end result.

``````var minValue = arr => parseInt(arr.sort().map((e,i,a) => {if( e != a[i+1])return e;}).filter(x=>x).toString().replace(/,/g, ""));
`````` Matt Ellen • Edited on
``````const minValue = ns => parseInt([...(new Set(ns))].sort().join(''));
`````` George WL • Edited on

Even works for [7,0,2], neat divyansh-pratap • Edited on

this is solution in c . I have first sorted the array then solved it using simple logics . suggestions are welcome .

# include

int sort(int A[] , int n );
int min(int A[] , int n);
int main()
{
int A;
int a , i , s;
printf("enter the number of elements you want to enter");
scanf("%d" ,&a);

``````for(i=0;i<a;i++)
{
printf("enter the %dth element" , i+1);
scanf("%d" , &A[i]);
}
s = min(A , a);
printf("/n %d" , s);
``````

}
int sort(int A[] , int n )
{
int i;
int temp , j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(A[j]<A[i])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
}

int min(int A[] ,int n)
{
int s , j=0 , k , i , l=1;

``````  sort(A , n);
s=A;
for(i=1;i<n;i++)
{

j=A[i];

for(k=0;k<i;k++)
{
if(A[i]==A[k])
{
l=0;
}
}
if(l==1)
{
s=s*10 + j;
}

l=1 ;

}

return s;
``````

} SavagePixie • Edited on

JavaScript

``````const formMin = arr => +Array
.from(new Set(arr))
.sort((a, b) => a - b)
.join('')
``````