
function numericSort(x,y) {  
   var z = new Number()       ;
   var a = Number(x)          ;
   var b = Number(y)          ;
   return(a - b)              ;
}

function ModeProblem(x) {
    this.the_mode= new String("") ;
    this.unique  = new Array();  // Array containing the unique values from the array list ... duplicates removed.
    this.copycnt = new Array();  // Array indicating the number of copies of the number in the unique array list.
    this.index  = new Number();  // index into the unique array.
    if ((x == undefined) || (x == null)) {
        x = "" ;
    }
    this.assign(x)            ;
}

ModeProblem.prototype.assign = function(x) {
    this.theList  = new List(x);  // Array containing the list being evaluated.
    this.the_mode = "" ;
    
    if (x != "") {
        (this.theList.listArray).sort(numericSort)  ;  // Sort the array that is to be evaluated.
    }
    
}

ModeProblem.prototype.count = function () {

    var match = new Number()   ;  // value of latest number being matched for duplication.
    var value = new Number()   ;  // value of the next number in the list.
    var index = new Number()   ;  // index for the uniqueness array.

    index   = 0                ;
    lastnum = 555555           ;  

    do { 
        value = Number((this.theList).iterate()) ;
        if (match == value) {
            this.copycnt[index]++ ;  // If this is a duplicate of the previous number, simply bump the count.                
        } else {
                                 // If this is not a duplicate of the previous number, save this as the first of 
                                 // a new value to be matched. 
            this.unique[++index] = value ; 
            this.copycnt[index]  = 1     ;
            match = value      ;
        }
    } while ((this.theList).eof() == false) ;

}

ModeProblem.prototype.evaluate = function () {

    var str = new String()    ;
    var i   = new Number()    ;
    
    var max    = new Number() ; // value of the count being matched.
    var index  = new Number() ; // Number of the answers available.
    var answer = new Array()  ; // An array of numbers with matching counts.
    
    index         = 1               ; 
    answer[index] = this.unique[1]  ; // Assume first thing is the winner.
    theMax        = this.copycnt[1] ; // with the maximum copies.
      
                                      // Now check that no other number has more matches
                                      // and determine if others do have matching counts.
    
    for (i = 2; i < (this.unique).length ; i++) {
        if (theMax < this.copycnt[i]) {
            answer.length = 0               ;
            index         = 1               ;
            answer[index] = this.unique[i]  ;
            theMax           = this.copycnt[i] ;
        } else if (this.copycnt[i] == theMax) {
            index++                         ;  
            answer[index] = this.unique[i]  ;
        }    
    } 
    str = "" ;
    for (i = 1; i < answer.length-1 ; i++) {
        str += answer[i] + ", " ;
    }
    str += answer[answer.length-1] ;
    this.the_mode = str
    return str ;
}

ModeProblem.prototype.mode = function () {
    return this.the_mode ;
}

