Noob: Remove duplicates from an int array
in
Programming Questions
•
1 year ago
Hello all,
I'm working on a data visualisation based on some EUROSTAT stats and I need to make an array listing the years to which the data refers.
I have managed to extract the whole column from the table into an array but I can't seem to get it to remove duplicates.
I'm posting the code for the function I'm trying to get to work.
My thought is to create a temporary array "temp[]" which will contain all the values in the column (lines 1–8), then create an empty "temp2[]" array in which values from temp[] are appended if they don't already exist in temp2[] (test in line 13).
I am very new to all this – and might be going about this entirely the wrong way – so I would appreciate it if answers/tips were a bit verbose to help me understand the principles behind whatever it is I'm meant to do...
Thanks in advance...
- int[] getColumnNoDup(Table tabName, int col) {
- int[] temp = new int[rowCount];
- int i = 0;
- for(int row = 1; row < rowCount-1; row ++) { //extracts whole column from table into temp[]
- temp[i] = tabName.getInt(row, col);
- i++;
- }
- int[] temp2 = new int[1];
- temp2[0] = temp[0];
- for(int x = 0; x < temp.length-1; x++) {
- for(int x2 = 0; x2 < temp2.length-1; x2++) { // cycle through temp2[]
- if(temp[x] == temp2[x2]) { // if value in temp[] already exists in temp2[] do nothing
- continue;
- }
- temp2 = append(temp2, temp[x]); // if value doesn't already exist in temp2[] append it
- }
- }
- return temp2;
- }
1