You could sort the array and then run through it and then see if the next (or previous) index is the same as the current. Assuming your sort algorithm is good, this should be less than
O(n2):
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
var sorted_arr = arr.sort(); // You can define the comparing function here.
// JS by default uses a crappy string compare.
var results = [];
for (var i = 0; i < arr.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
alert(results);
// in a function
function compare(arr) {
var sorted_arr, results, i, len;
sorted_arr = arr.sort();
results = [];
for (i = 0, len = arr.length; i < len - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
The
.append()method is perhaps the most misused of all jQuery methods. While an extremely useful and easy method to work with, it dramatically affects the performance of your page. When misused, the.append()method can cripple your JavaScript code's performance. When used well, it'll keep your script humming along.
var arr = reallyLongArray;
$.each(arr, function(count, item) {
var newTr = '<tr><td name="pieTD">' + item + '</td></tr>';
$('table').append(newTr);
});
Ran Once: Profile (107.458ms, 3991 calls, Firefox 3.06)
Loop of 100: Profile (21641.336ms, 399100 cal
var arr = reallyLongArray;
var textToInsert = '';
$.each(arr, function(count, item) {
textToInsert += '<tr><td name="pieTD">' + item + '</td></tr>';
});
$('table').append(textToInsert);
Ran Once: Profile (30.792ms, 778 calls, Firefox 3.06)
Loop of 100: Profile (8505.37ms, 77800 calls)
Taking full advantage of the ability of jQuery to insert a chunk of html in a string means only having to call insert once. It's much quicker, with an approximately 9-10x speed increase from the initial algorithm! This will be fast enough for 95% of cases, but for strings with lots of string concatenation… Wait, there's more!
full article at learningjquery.com
You can do this easily with some nice Jquery Plugins like nicescroll, or you can do the thing with css. But it only works on webkit browsers, Here is how:
::-webkit-scrollbar {
width: 12px;
}
/* Track */
::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
-webkit-border-radius: 10px;
border-radius: 10px;
}
/* Handle */
::-webkit-scrollbar-thumb {
-webkit-border-radius: 10px;
border-radius: 10px;
background: rgba(255,0,0,0.8);
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
::-webkit-scrollbar-thumb:window-inactive {
background: rgba(255,0,0,0.4);
}
.top-box {
box-shadow: inset 0 7px 9px -7px rgba(0,0,0,0.4);
}
.left-box {
box-shadow: inset 7px 0 9px -7px rgba(0,0,0,0.4);
}
.right-box {
box-shadow: inset -7px 0 9px -7px rgba(0,0,0,0.4);
}
.bottom-box {
box-shadow: inset 0 -7px 9px -7px rgba(0,0,0,0.4);
}
There are some examples on the Mozilla Developer Center page:
/**
* Returns a random number
* between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer
* between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}