JSFiddle - React, Tailwind, and code Playground

by CommandLineDesign

JavaScript

// ::::::::::::::::::::::::::::::::::::::::::::: 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
         ...