Let’s start with the trivial case, this problem could easily be solved by merging the two arrays into a single array. Merging can use the merge portion ofmerge sort which runs in O(n)O(n) time, and then the median could be retrieved by grabbing the middle element(s) in O(1)O(1) time. But is it possible to get the median using a sub-linear solution?
Tag Archives: Arrays
I was surprised at how surprising some found this. Quite a few people suggested the
reduce version could be changed to not do the array copy (I don’t think it can). Some suggested maybe
+ could be optimized so it doesn’t perform a copy (I don’t think that it could easily, as we’ll see shortly).1
In other feedback, a few commented on the previous post about linked lists. Why implement an outdated data structure? What’s the point when we have arrays?
Lesson 13 of the CoffeeScript foundations course
Stage: Loops, Arrays and Objects
In this lesson we’ll talk about some useful methods for Arrays and also we’ll talk about Two-dimensional arrays and how to work with them, we also have a #CoffeeChallenge and #CoffeeQuiz!
Once you solve the challenge or the quiz of this lesson drop us a line on twitter with the #CoffeeChallenge and #CoffeeQuiz hashtag!
VER VERSIÓN EN ESPAÑOL: http://bit.ly/1K1j98z
And of course subscribe if you want us to upload more videos.
Thanks for your support!
Typed array views act like single-type arrays to a segment of an ArrayBuffer. There are views for all the usual numeric types, with self-descriptive names like Float32Array, Float64Array, Int32Array and Uint8Array. There’s also a special view which has replaced the pixel array type in Canvas’s ImageData: Uint8ClampedArray.
DataView is the second type of view and it is meant for handling heterogeneous data. Instead of having an array-like API, the DataView object provides you a get/set API to read and write arbitrary data types at arbitrary byte offsets. DataView works great for reading and writing file headers and other such struct-like data.
We’ve talked before about the advantages of using
Array‘s higher order functions (like
sort) rather than manually iterating over an array’s contents. But unlike
sort, which produce new arrays,
reduce can boil an array down to a new value of any type.
Consider the case of a library system which needs to show potential donors its effectiveness at spreading knowledge. In order to do that, it must determine how many book-checkouts there were last year. (Not total of all checkouts, since a single checkout may be for more than one book.)
The array object is indeed very powerful, but you need to handle it with care since some quite innocent feature can be have a high time and/or garbage creation cost.
I’ll show here a few simple techniques that avoids those costs.
The performance benefits can be very high (more than 10X) for some features, so all this is worth a look.
- A Sample Class
- Object Life Cycle
- Control Structures
- Deﬁnite Loops (for)