
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Smallest number formed by shuffling one digit at most in JavaScript
Problem
We are required to write a JavaScript function that takes in a positive number n. We can do at most one operation −
Choosing the index of a digit in the number, remove this digit at that index and insert it back to another or at the same place in the number in order to find the smallest number we can get.
Our function should return this smallest number.
Example
Following is the code −
const num = 354166; const smallestShuffle = (num) => { const arr = String(num).split(''); const { ind } = arr.reduce((acc, val, index) => { let { value, ind } = acc; if(value > val){ value = val; ind = index; }; return { value, ind }; }, { value: Infinity, ind: -1 }); const [item] = arr.splice(ind, 1); arr.unshift(item); return Number(arr.join('')); }; console.log(smallestShuffle(num));
Output
Following is the console output −
135466
- Related Articles
- Form the smallest number using at most one swap operation in C++
- Finding the maximum number using at most one swap in JavaScript
- JavaScript - Find the smallest n digit number or greater
- Checking digit sum of smallest number in the array in JavaScript
- Python Program for Smallest K digit number divisible by X
- C++ Programming for Smallest K digit number divisible by X?
- C++ Program for Smallest K digit number divisible by X?
- Java Program for Smallest K digit number divisible by X
- Forming palindrome using at most one deletion in JavaScript
- Finding difference of greatest and the smallest digit in a number - JavaScript
- The digit at the ten\'s place of a two digit number is four times the digit at one\'s place. If the sum of this number and the number formed by reversing the digits is 55, find the numbers.
- Find longest palindrome formed by removing or shuffling chars from string in Python
- Find longest palindrome formed by removing or shuffling chars from string in C++
- Numbers At Most N Given Digit Set in C++
- Is the digit divisible by the previous digit of the number in JavaScript

Advertisements