c# - Deleting Duplicates From An Array -
i have code load text file , save array. array contains list of numbers of numbers being duplicated. in code first loop through array , replace duplicated numbers -1. plan on deleting -1 values array. remaining array values (non duplicates) copied on new array outputted.
however keep getting error when attempt delete -1 values array (see code below). don't know why happening if knows please let me know!
p.s. school project can use loops , if statements, not things linq or foreach, etc.
public partial class form1 : form { //global variable int[] original; public form1() { initializecomponent(); } //exit application private void mnuexit_click_1(object sender, eventargs e) { this.close(); } //load file private void mnuload_click_1(object sender, eventargs e) { //code load numbers file openfiledialog fd = new openfiledialog(); //open file dialog , check if file selected if (fd.showdialog() == dialogresult.ok) { //open file read streamreader sr = new streamreader(fd.openfile()); int records = int.parse(sr.readline()); //assign array sizes original = new int[records]; //go through text file (int = 0; < records; i++) { original[i] = int.parse(sr.readline()); } } } private void btnoutput_click(object sender, eventargs e) { //store original array string output = "original \n"; //output original array (int = 0; < original.length; i++) { output = output + original[i] + "\n"; } //create temparray int[] temparray = new int[original.length]; //set temparray equal original array (int = 0; < original.length; i++) { temparray[i] = original[i]; } //duplicate number counter int counter = 0; //loop through entire array (int = 0; < temparray.length; i++) { (int j = + 1; j < temparray.length; j++) { //replace duplicate values '-1' if (temparray[i] == temparray[j]) { temparray[j] = -1; counter++; } } } //set size of original array original = new int[original.length - counter]; //remove -1 values //index counter int index = 0; //error starts (int = 0; < temparray.length; i++) { if (temparray[i] != -1) { original[index] = temparray[i]; index++; } } //error ends //final output -- new array output = output + "original without duplicates\n"; (int = 0; < original.length; i++) { output = output + original[i] + "\n"; } lbloutput.text = output; } } }
reverse line temparray[i] = original[index];
original[index] = temparray[i];
Comments
Post a Comment