function filter(arr) {
  var filteredArray = [], prev;
  arr.sort();
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] !== prev) {
      filteredArray.push(arr[i]);
    }
    prev = arr[i];
  }
  return filteredArray
}

function rarefy(pattern) {
  arr = [];
  matches = [];
  merged = [];
  str = text.doc;
  elements = xml.doc.querySelectorAll("[category=" + pattern + "]")
  for (var i = 0; i < elements.length; i++) {
    arr.push(elements[i].textContent.replace(/(\r\n|\n|\r)/gm,"").trim());
  }
  filteredArray = filter(arr);
  return filteredArray
}

function countMatches(pattern) {
  filteredArray = rarefy(pattern);
  for (var i = 0; i < filteredArray.length; i++) {
    pattern = filteredArray[i];
    regex = new RegExp(pattern, 'gi');
    if (str.match(regex)) {
      matches.push(str.match(regex));
    }
  }
  merged = merged.concat.apply(merged, matches); // For unnesting nested arrays
  return merged.length;
}


function filter(arr) {
  var filteredArray = [], prev;
  arr.sort();
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] !== prev) {
      filteredArray.push(arr[i]);
    }
    prev = arr[i];
  }
  return filteredArray
}

function rarefyXMLTextContent(pattern) {
  arr = [];
  matches = [];
  merged = [];
  str = text.doc;
  elements = xml.doc.querySelectorAll("[category=" + pattern + "]")
  for (var i = 0; i < elements.length; i++) {
    arr.push(elements[i].textContent.replace(/(\r\n|\n|\r)/gm,"").trim());
  }
  filteredArray = filter(arr);
  return filteredArray
}

function countMatches(pattern) {
  filteredArray = rarefyXMLTextContent(pattern);
  for (var i = 0; i < filteredArray.length; i++) {
    pattern = filteredArray[i];
    regex = new RegExp(pattern, 'gi');
    if (str.match(regex)) {
      matches.push(str.match(regex));
    }
  }
  merged = merged.concat.apply(merged, matches); // For unnesting nested arrays
  return merged.length;
}


Rename helper methods for clarity
Rename helper methods for clarity
