Another way would be to store the NTE-3 value in a parameter map and then remove the three lines following the duplicate "Electronically Signed" text.
// remove every NTE segment
message.removeAllNodes('NTE');
// store the original NTE segs as an array
var nteSegs = source.getAllNodes("NTE");
var nteMap = qie.newParameterMap();
// cycle each NTE segment and only add not duplicate
for (var i=0 ; i < nteSegs.length ; i++) {
qie.debug(nteSegs[i]);
var nteSeg = qie.parseHL7String(nteSegs[i]);
var nte3 = nteSeg.getNode('NTE-3');
if (nte3.length() > 0 && nte3 + '' !== '_~') {
//need to check in the nteSegs array if this current NTE segment exists.
if (nteMap.get(nte3) === null) {
message.addChild('/', 'NTE', nteSeg);
nteMap.put(nte3, true);
} else if (StringUtils.contains(nte3, 'Electronically Signed')) {
// So this nte3 was already found once so now remove the next three lines by skipping them.
i = i + 2;
}
} else {
//go ahead and add blank lines or lines that equal '~'
message.addChild('/', 'NTE', nteSeg);
}
}