What is the Efficient way to insert a number into a sorted array of numbers in JavaScript ? Last Updated : 23 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Given an array of numbers and the task is to insert a number into the sorted array using JavaScript. There are many approaches to solving this problem two of which are given below: Approach 1: First, take the values in a variable (lets arr).Make sure it is sorted.In this example, the complexity is O(n), where n is the number of elements in the array available.The method findLoc is searching for the element just greater than the element that we want to insert.The method returns the index of the location.perform the insert operation using the .splice() method. Example: This example illustrates the approach discussed above. html <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="gfg_Run()"> Insert </button> <p id="GFG_DOWN"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = new Date(); var arr = [1, 2, 4, 6, 9]; el_up.innerHTML = "Click on the button to insert a number "+ "in javascript array.<br> Array is = " + arr; function add(el, arr) { arr.splice(findLoc(el, arr) + 1, 0, el); return arr; } function findLoc(el, arr, st, en) { st = st || 0; en = en || arr.length; for (i = 0; i < arr.length; i++) { if (arr[i] > el) return i - 1; } return en; } function gfg_Run() { add(7, arr); el_down.innerHTML = "Array becomes " + arr; } </script> Output: What is the Efficient way to insert a number into a sorted array of numbers in JavaScript? Approach 2: In this example, the complexity is O(Logn), where n is the number of elements in the array.A method findLoc is searching for the location where the element should be present.Method returns the index of the location by using binary search algorithm.perform the insert operation using .splice() method. Example: This example illustrate the approach discussed above. html <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Insert </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = new Date(); var arr = [1, 2, 4, 6, 9]; el_up.innerHTML = "Click on the button to insert a "+ "number in javascript array.<br> Array is = " + arr; function add(el, arr) { arr.splice(findLoc(el, arr) + 1, 0, el); return arr; } function findLoc(el, arr, st, en) { st = st || 0; en = en || arr.length; var pivot = parseInt(st + (en - st) / 2, 10); if (en - st <= 1 || arr[pivot] === el) return pivot; if (arr[pivot] < el) { return findLoc(el, arr, pivot, en); } else { return findLoc(el, arr, st, pivot); } } function gfg_Run() { add(5, arr); el_down.innerHTML = "Array becomes " + arr; } </script> Output: What is the Efficient way to insert a number into a sorted array of numbers in JavaScript? Comment More infoAdvertise with us Next Article What is the Efficient way to insert a number into a sorted array of numbers in JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads What is the most efficient way to create a zero filled array in JavaScript ? Creating zero-filled arrays is a common task in JavaScript, essential for initializing data structures, matrices, and more. This guide explores the most efficient methods to create zero-filled arrays, enhancing performance and code readability. Syntax: arr(length).fill(value);Importance of Zero-Fill 2 min read What is the most efficient way to concatenate N arrays in JavaScript ? In this article, we will see how to concatenate N arrays in JavaScript. The efficient way to concatenate N arrays can depend on the number of arrays and the size of arrays. To concatenate N arrays, we use the following methods:Table of ContentMethod 1: Using push() MethodMethod 2: Using concat() Met 3 min read How to move specified number of elements to the end of an array in JavaScript ? The purpose of this article is to move some specified elements to the end of an array using JavaScript.Given an array of length say N, move some specified amount of elements say X to the end of the given array.Input:arr = [1, 2, 3, 4, 5]X = 2Output: The following array should be the output as the fi 3 min read What is the fastest way to loop through an array in JavaScript ? The fastest way to loop through an array in JavaScript depends upon the usecases and requirements. JavaScript has a large variety of loops available for performing iterations. The following loops along with their syntax are supported by JavaScript. Table of Content for loop while loop .forEach() loo 3 min read How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati 4 min read How to create dynamic length array with numbers and sum the numbers using JavaScript ? In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable. Unlike most languages where the array is a reference to the multiple variables, in JavaScript array is a single variable 2 min read How to get the standard deviation of an array of numbers using JavaScript ? Given an array and the task is to calculate the standard deviation using JavaScript. Example: Input: [1, 2, 3, 4, 5]Output: 1.4142135623730951Input: [23, 4, 6, 457, 65, 7, 45, 8]Output: 145.13565852332775Please refer to Mean, Variance, and Standard Deviation for details. Mean is average of element. 3 min read How to get index at which the value should be inserted into sorted array based on iterator function in JavaScript ? In this article, we will learn how to get an index at which the value should be inserted into a sorted array based on the iterator function in JavaScript. Here we use the sorting function to sort the array and we would print the array and the position of the new element in the DOM.Approach:Create an 4 min read What are the different operations can be performed with a JavaScript Array? In this article, we are going to learn different operations that can be performed with an Array by using JavaScript, An array in JavaScript is a data structure that holds an ordered collection of values, which can be of any data type, using zero-based indexing for access and manipulation, including 5 min read How to write a function that returns array elements larger than a number in JavaScript ? Given an array arr and number n and the task is to write a function that returns an array whose items are larger than n. Example: Input: arr = [65, 16, 0, 6, 64, 1, 68] n = 16 Output: [65, 64, 68] Input: arr = [6, 46, 54, 6, 56, 54, 65, 4, 65] n = 50 Output: [54, 56, 54, 65, 65] To achieve this we h 4 min read Like