#8009

Yutaka Emura
Keymaster

Then how about this?


function Pair( i, s )
{
this.index = i;
this.str = s;
}

nLines = document.GetLines();

// Create an array
a = new Array( nLines );

status = "Reading lines..."

// Fill the array a with all lines (with returns) in the document.
for( i = 1; i <= nLines; i++ ) {
if( (i \% 1000) == 0 ){
status = "Reading lines: " + String(i + 1) + "/" + String(nLines);
}
var pair = new Pair( i, document.GetLine( i, eeGetLineWithNewLines ) );
a.push( pair );
}

status = "Sorting lines..."

a.sort( function(a,b){
if( a.str > b.str ){
return 1;
}
if( a.str < b.str ){
return -1;
}
return a.index - b.index;
});

// Delete duplicate elements.
for( i = 1; i < nLines; i++ ){
if( (i \% 10) == 0 ){
status = "Deleting duplicate lines: " + String(i + 1) + "/" + String(nLines);
}
if( a[i].str == a[i-1].str ){
a[i].index = 0; // disable
}
}

status = "Sorting lines again..."

a.sort( function(a,b){
return a.index - b.index;
});

var str = "";
n = 0;
for( i = 0; i < nLines; i++ ){
if( a[i].index != 0 ){
if( (i \% 1000) == 0 ){
status = "Joining lines: " + String(i + 1) + "/" + String(nLines);
}
str += a[i].str;
}
else {
n++;
}
}

// Replace the entire document with new elements
document.selection.SelectAll();
document.selection.Text = str;
status = n + " duplicate lines deleteded."