I have data in a 2d array, of Objects. I am looking for an algorithm which combines duplicate data, but my algorithm loses the ones which are duplicates. Also, it keeps all the rows on the top, so, my data is:
ECE 201 Electrical and Computer Engineering I 2.6666666666666665
BIO 101 Biology I 2.0
CS 101 Computer Science I 3.5
AE 101 Aerospace Engineering I 2.5
BE 101 Biomedical Engineering I 2.0
CS 101 Computer Science I 2.0
CS 102 Computer Science II 3.0
BE 100 Intro Biomedical Engineering 3.6
ECE 200 Intro Electrical and Computer Engineering 3.5
BIO 100 Intro Biology 2.2
CS 101 Computer Science I 3.75
AE 101 Aerospace Engineering I 1.6666666666666667
BE 100 Intro Biomedical Engineering 1.2
but it is filled with nulls. I use the following algorithm:
for (int i = 0; i< finalData.length; i++)
{
if (finalData[i][1] != null)
{
String first = (String)finalData[i][1];
for (int j = i; j< finalData.length; j++)
{
if (finalData[j][1]!= null)
{
String second = (String)finalData[j][1];
if (first.equals(second))
{
double one = (double)finalData[i][6];
double two = (double)finalData[i][6];
finalData[i][7] = (one + two)/2;
System.out.println("found!\t" + first + "\t" + second + "\t" + finalData[i][7]);
}
}
}
}
}
and my data becomes:
found! ECE 201 ECE 201 2.6666666666666665
found! BIO 101 BIO 101 2.0
found! CS 101 CS 101 3.5
found! CS 101 CS 101 3.5
found! CS 101 CS 101 3.5
found! AE 101 AE 101 2.5
found! AE 101 AE 101 2.5
found! BE 101 BE 101 2.0
found! CS 101 CS 101 2.0
found! CS 101 CS 101 2.0
found! CS 102 CS 102 3.0
found! BE 100 BE 100 3.6
found! BE 100 BE 100 3.6
found! ECE 200 ECE 200 3.5
found! BIO 100 BIO 100 2.2
found! CS 101 CS 101 3.75
found! AE 101 AE 101 1.6666666666666667
found! BE 100 BE 100 1.2
//the found! is just for making sure that it was the right output.
How can I improve this algorithm to produce the desired result?
Instead of writing an algorithm, you can use a set or a hash table.
Sets are your better choice; they do not allow duplication. You can read more about them here:
http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
Hash tables are a way to implement sets. To use these, you would need to write a hashing function to turn your data into a hash key, then you would use that key to index into the table and store the unhashed value there.