-
Notifications
You must be signed in to change notification settings - Fork 0
/
7kyu-formTheMinimum.js
32 lines (26 loc) · 1.26 KB
/
7kyu-formTheMinimum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Task
Given a list of digits, return the smallest number that could be formed from these digits, using the digits only once (ignore duplicates).
Notes:
Only positive integers will be passed to the function (> 0 ), no negatives or zeros.
Input >> Output Examples
minValue ({1, 3, 1}) ==> return (13)
Explanation:
(13) is the minimum number could be formed from {1, 3, 1} , Without duplications
minValue({5, 7, 5, 9, 7}) ==> return (579)
Explanation:
(579) is the minimum number could be formed from {5, 7, 5, 9, 7} , Without duplications
minValue({1, 9, 3, 1, 7, 4, 6, 6, 7}) return ==> (134679)
Explanation:
(134679) is the minimum number could be formed from {1, 9, 3, 1, 7, 4, 6, 6, 7} , Without duplications
*/
//P: one input, an array of positive integers > 0
//R: return the smallest possible number that could be formed from these digits, using each digit exactly once and ignoring duplicates
//E: [1, 3, 1] => 13
// [5, 7, 5, 9, 7] => 579
// [1, 9, 3, 1, 7, 4, 6, 6, 7] => 134679
//P: create a new set of nums from the input to remove dupes, sort in ascending order, join them into a string and return the string converted back into a num
function minValue(values){
let noDupes = [...new Set(values)];
return Number(noDupes.sort((a,b) => a-b).join(''))
}