JavaScript Program to Construct Largest Number from Digits
Last Updated :
25 Oct, 2023
In this article, we have given a set of digits, and our task is to construct the largest number generated through the combination of these digits. Below is an example for a better understanding of the problem statement.
Example:
Input: arr[] = {4, 9, 2, 5, 0}
Output: Largest Number: 95420
Using sort() Method
The sort function is used to sort the input digits either in ascending or descending order. As we need to construct the largest number, we will sort the input data in the descending order and then concatenate the sorted digits to construct the largest number.
Syntax:
digit.sort((a,b) => b-a);
Example: In this example, we will construct largest number from numbers using sort() method.
JavaScript
let inputNumbers = [8, 3, 4, 7, 9];
inputNumbers.sort((a, b) => b - a);
let largestNumber =
inputNumbers.join("");
console.log(largestNumber);
The Math.max method is used to find the largest number from the given numbers. We are randomly finding the maximum digit from the input set of numbers and storing the result in a new variable. We are using the Math.max method to find the maximum digit from the set of numbers.
Syntax:
Math.max([value1[, value2[, ...]]])
Example: In this example, we will construct the largest number from the digits using the Math.max() method.
JavaScript
let inputNumber = [8, 3, 4, 7, 9];
let largestNumber = "";
while (inputNumber.length > 0) {
let largestDigit = Math.max(
...inputNumber
);
largestNumber +=
largestDigit.toString();
inputNumber.splice(
inputNumber.indexOf(
largestDigit
),
1
);
}
console.log(largestNumber);
Using Loops
The loops in JavaScript can be used to select the largest number from a set of digits. We are creating an empty variable to store the largest number, then we are repeatedly finding the largest digit in the set of digits, then we are adding this largest digit to the variable and retrieving it from the array of input digits.
Syntax:
while(condtion) {
//statements
for(condition) {
if(condition) {
//statements
}}}
Example: In this example, we will construct the largest number from digits using looping in JavaScript.
JavaScript
let inputNumber = [8, 3, 4, 7, 9];
let largest = "";
while (inputNumber.length > 0) {
let largestDigit = -1;
let largestDigitIndex = -1;
for (
let i = 0;
i < inputNumber.length;
i++
) {
if (
inputNumber[i] >=
largestDigit
) {
largestDigit =
inputNumber[i];
largestDigitIndex = i;
}
}
largest += largestDigit.toString();
inputNumber.splice(
largestDigitIndex,
1
);
}
console.log(largest);
Using Array
The array is used to select the largest number from the data here. We are creating array names as "temp" that consist of 10 elements initialized to zero, where each of the elements corresponds to the digit [0-9]. Then we are iterating over the input numbers array and incrementing the count of each digit correlation in the "temp" array. There is an empty string, "largestNumber," that stores the final result. We iterate in the reverse order, from 9 down to 0. For each digit [i], we are checking the count raised in [i]. Then it appends the digit 'i' to the largest number, t[i] times, to construct the largest number.
Syntax:
let temp = Array(10).fill(0);
Example: In this example, we will construct the largest number from the digits using the Array.
JavaScript
let inputNumbers = [8, 3, 4, 7, 9];
let length = inputNumbers.length;
let temp = Array(10).fill(0);
for (let i = 0; i < length; i++) {
temp[inputNumbers[i]]++;
}
let largestNumber = '';
for (let i = 9; i >= 0; i--) {
while (temp[i]-- > 0) {
largestNumber += String(i);
}
}
console.log(largestNumber);
Similar Reads
JavaScript Program to Find Largest Number in a Linked List
Finding the largest number in a linked list is a common problem and can be efficiently solved using both iterative and recursive approaches. A linked list is a fundamental data structure where each element, known as a node, is connected to the next one, forming a chain. Table of Content Iterative Ap
3 min read
JavaScript Program to Handle Number Overflow
Number overflow occurs when the result of a mathematical operation exceeds the maximum representable value for a numeric data type. In JavaScript, this limit is reached when dealing with numbers beyond the safe range defined by the Number.MAX_SAFE_INTEGER constant, which is approximately 9 quadrilli
2 min read
JavaScript Program to Multiply the Given Number by 2 such that it is Divisible by 10
In this article, we are going to implement a program through which we can find the minimum number of operations needed to make a number divisible by 10. We have to multiply it by 2 so that the resulting number will be divisible by 10. Our task is to calculate the minimum number of operations needed
3 min read
JavaScript Program for Armstrong Numbers
In this article, we will see a JavaScript program to check whether the given number is an Armstrong number or not. An Armstrong Number is an n-digit number that is the sum of the nth power of its all digits. For instance, Consider a 3-digit number, i.e., 153, which is a 3-digit number, & the sum
4 min read
Javascript Program to Rotate digits of a given number by K
Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required
2 min read
JavaScript Program to Find Largest Element in an Array
In this article, we are going to learn about the largest element in an array in JavaScript. The largest element in an array refers to the value that holds the greatest numerical or lexicographic (string) order among all elements present in the array. Example: Input : [10, 15, 38, 20, 13];Output: 38H
3 min read
Javascript Program to Find Maximum value possible by rotating digits of a given number
Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati
2 min read
What is the largest 3 digit whole number?
Numerals are the mathematical figures used in financial, professional as well as a social field in the social world. The digits and place value in the number and the base of the number system determine the value of a number. Numbers are used in various mathematical operations as summation, subtracti
5 min read
Program to generate a random single digit number
Write a program to generate a random single-digit number. Example 1: 7Example 2: 3 Approach: To solve the problem, follow the below idea: To generate a random single-digit number, we can first generate a random integer and then take apply modulo to get the last digit of that random integer. This las
2 min read
Java Program to Return the Largest Element in a List
Given a List, find the largest element in it. There are multiple approaches to tackle this problem, such as iterating through the List or using various inbuilt functions. Input : List = [5, 3, 234, 114, 154] Output : 234 Input : List = {10, 20, 4} Output : 20Approach 1: Using a ForEach Loop Create L
3 min read