简体   繁体   English

在 JavaScript 数组中查找最后一个元素的计算效率最高的方法?

[英]Most computationally efficient way to find last element in a JavaScript Array?

Would it be faster to use难道是更快地使用

var lastelement = myarray[myarray.length - 1];

or

var lastelement = myarray.reverse()[0];

and why?为什么?

Just think about it.想想看。

If you know how long an array is, it is much faster to just get the last value than to have to compute the reverse!如果您知道数组的长度,那么获取最后一个值比必须反向计算快得多!

It is faster to access an element by its index, as it should have O(1) complexity.通过索引访问元素更快,因为它应该具有 O(1) 复杂度。 Reversing an array and then accessing the first index, on the other hand, would have at least O(n) complexity, depending on how the reversing algorithm is implemented.另一方面,反转数组然后访问第一个索引至少具有 O(n) 复杂度,具体取决于反转算法的实现方式。

I'll take this from a different angle than has been answered here.比已经在这里找到答案,我会从不同的角度采取这种。 Chances are you just want to get the last element, you don't want to do anything to the actual array itself.有可能到头来你只是想获得的最后一个元素,你不想做任何事情,以实际数组本身。 If you use array.reverse to get the last element, you are actually changing the array (probably an unpleasant side effect in your case).如果您使用array.reverse获得的最后一个元素,你实际上改变的是阵列(可能你的情况不愉快的副作用)。

var myArray = [.....]; // some array
var lastElement = myArray.reverse()[0]; // get the last element
var firstElement = myArray[0]; // tricked you! This is now the same as
                               // lastElement because the myArray object
                               // in memory has been reversed.

So if you want to get the last element without changing the array you'd have to do this:所以,如果你想获得最后一个元素,而不改变阵列,你不得不这样做:

var myArray = [.....]; // some array
var lastElement = myArray.slice().reverse()[0]; // copy and get the last element
var firstElement = myArray[0]; // this is the correct first element

Pretty obvious which way is more efficient now.很明显哪种方式现在是更有效的。

Array.reverse() replacing old array with new reversed array in same reference ,crating the new array with new elements is much slower than get array element by index. Array.reverse()与相同的参考新反转阵列更换旧阵列,板条箱以新元素的新阵列比由索引GET数组元素慢得多。 var lastelement = myarray[myarray.length - 1]; VAR是以lastElement = myArray的[myarray.length - 1]; Is much faster.要快得多。

The real efficient solution is to use a red and black binary tree ( http://en.wikipedia.org/wiki/Red%E2%80%93black_tree ) where you'll store in each node one array value, the delta with the previous and next item, as well as the relative position to median.真正有效的解决方案是使用红黑二叉树 ( http://en.wikipedia.org/wiki/Red%E2%80%93black_tree ),您将在其中在每个节点中存储一个数组值,即上一项和下一项,以及与中位数的相对位置。 Then with only a few traversal you should be able to identify the last element (last element is the one having an index with biggest spread to median index, while having a previous item and no next item).然后只需几次遍历,您就应该能够识别最后一个元素(最后一个元素是具有与中值索引的最大分布的索引,同时具有前一项但没有下一项的元素)。

The trick is that each traversal takes only O(ln(n)) and since you do less than ln(n) traversal, time is below O(sq(ln(n))), so it's very fast.诀窍是每次遍历只需要 O(ln(n)) 并且因为你做的遍历少于 ln(n),所以时间低于 O(sq(ln(n))),所以它非常快。

Edit : following Bergi's constructive comments, i just wonder if it wouldn't be faster to just use arrays for this, by using a Fast fourier transform on an array containing all indexes of the array, then identifying last element with a frequency analysis, then convert back the array to get the number out of the frequency.编辑:遵循 Bergi 的建设性评论,我只是想知道仅使用数组是否会更快,方法是在包含数组所有索引的数组上使用快速傅立叶变换,然后使用频率分析识别最后一个元素,然后转换回数组以获取频率之外的数字。 I apologize if i'm unclear, i don't see clearly all steps at the time of writing, but i think it's a idea worth following.如果我不清楚,我很抱歉,在撰写本文时我没有清楚地看到所有步骤,但我认为这是一个值得遵循的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM