// ::::::::::::::::::::::::::::::::::::::::::::: Code To Test :::::::::::::::::::::::::::::::::::::::::::::
var findDuplicates = function(int_list){
var n = int_list.length - 1;
var position_in_cycle = n + 1;
for(_ in xrange(n)){
position_in_cycle = int_list[position_in_cycle - 1];
}
var remembered_position_in_cycle = position_in_cycle;
var current_position_in_cycle = int_list[position_in_cycle - 1];
var cycle_step_count = 1;
while(current_position_in_cycle != remembered_position_in_cycle){
current_position_in_cycle = int_list[current_position_in_cycle - 1];
cycle_step_count += 1;
}
var pointer_start = n + 1;
var pointer_ahead = n + 1;
for(_ in xrange(cycle_step_count)){
pointer_ahead = int_list[pointer_ahead - 1];
}
while(pointer_start != pointer_ahead){
pointer_start = int_list[pointer_start - 1];
pointer_ahead = int_list[pointer_ahead - 1];
}
return pointer_start
}
// ::::::::::::::::::::::::::::::::::::::::::::: Utilities, Polyfills :::::::::::::::::::::::::::::::::::::::::::::
var xrange = function(b0, b1, quanta) {
if (!quanta) { quanta = 1; }
if (!b1) { b1 = b0; b0 = 0; }
out = [];
for (var i = b0, idx = 0; i < b1; i += quanta, idx++) {
out[idx] = i;
}
return out;
}
// ::::::::::::::::::::::::::::::::::::::::::::: Testing :::::::::::::::::::::::::::::::::::::::::::::
// :: Define Tests ::
// *** Generate a list of integers 0-n, add one duplicate
var DuplicateIntegerList = function(n){
this.getRandomNumber = function(){
// Get a random int less than n
return Math.floor(Math.random() * n);
};
//Populates a list with random numbers from 1-n
this.populateNumberList = function(){
var result = [];
while(result.length < (n)){
var number = this.getRandomNumber();
debugger;
if(result.indexOf(number) === -1){
//Make sure there are no duplicates for the raw list, add if not dupe
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.