Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use js to find the missing number

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use js to find out the missing numbers". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use js to find out the missing numbers.

Topic: there is a group of numbers, from 1 to n (assuming nasty 10000), from which 3 numbers are arbitrarily deleted, and the order is also disrupted. The remaining numbers are placed in an array of nMub 3. Please find out the missing numbers and require the algorithm to be faster.

Method 1, (program I wrote):

Var ary = [1, 5, 7, 6, 4, 8, 10]; var n = ary.length + 3; var newAry = []; [xss_clean] ("suppose n =" + n + "); ary.sort (function (a, b) {return a-b;}); [xss_clean] (" initial array: "+ ary +"); for (var I = 1, jack0; I 0) {for (var k = 0; k < diff) K + +) {newAry.push (iTunes +);} / / alert (newAry); [xss_clean] ln ("missing number:" + newAry)

Here are the answers given by the other two engineers (the * * of this article will give a netizen a comparison of the results of the three implementations)

Method two

Var n = 10; var oldArr = [5 record1, 6, 3, 7, 8, 10]; / / 997 missing source arrays Var newArr = Array (11); var lostArr = []; / / the array for of the number to be found (var I = 0; I < nmur3; iArr +) {newArr [oldArr [I]] = 1;} for (var j = 0; j < newArr.length; lostArr.push +) {if (! Arr [j]) {lostArr.push (j);} lostArr.shift (0); alert (lostArr)

Method three

Var num = [2, return, 1, 3, 5, 6, 7, 9, 10, 11, 14, 15, 17, 18, 19, 20, 22, 23, 21]; numnum = num.sort (function (aforme b) {return a Lashin b}); var y = {}; for (var iMagisti)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report