1var MajorityChecker = function (arr) {2this.arr = arr;3};45MajorityChecker.prototype.query = function (left, right, threshold) {6var candidate = 0;7var count = 0;8for (var i = left; i < right + 1; i++) {9if (count == 0) {10candidate = this.arr[i];11count = 1;12} else if (candidate == this.arr[i]) {13count += 1;14} else {15count -= 1;16}17}1819count = 0;20for (var i = left; i < right + 1; i++) {21if (candidate == this.arr[i]) {22count += 1;23}24}2526if (count >= threshold) {27return candidate;28}29return -1;30};