java - Copying parallel arrays to new parallel arrays without duplicates -
i writing method takes 2 parallel arrays , int size variable keep track of indexing. example
string[] names = new string[100]; //partially filled array int[] scores = new int[names.length]; //scores each plater int entry = 0; names[size] = "bob"; scores[size] = 20; size++;
the above code indicates on first entry in "list" tis of bob has score of 20.
i writing method takes names array, scores array, , size variable. must copy information of arrays passed on new arrays duplicate entries 1 entry , sum aligned.
so example if there original list
name: bob, score: 20 name: paul, score: 30 name: bob, score: 10 //size - 3
so new parallel arrays must
name: bob, score: 30 name: paul, score 30 //size - 2
i have created method, , add sum of names, stuck on process of removing duplicates.
public static void totaldurations(string[] oldnames, int[] oldscores, int oldsize) { string[] newnames = new string[100]; int[] newscores = new int[newnames.length]; int newsize = 0; int matchpos; int addduration = 0; string tempstring = null; (int = 0; < oldsize; i++) { //the find method returns index of string if found else returns -1 matchpos = find(oldnames, oldsize, 0, oldnames[i]); while (matchpos >= 0) { addduration += oldscores[matchpos]; // find next match, starting after last 1 matchpos = find(oldnames, oldsize, matchpos + 1, oldnames[i]); } newnames[newsize] = oldnames[i]; newscores[newsize] = addduration; addduration = 0; newsize++; } //print list (int = 0; < newsize; i++) { system.out.println("name: " + newnames[i] + ", score: " + newscores[i] ); } }
if pass entries
"bob", 10 "bob", 10 "paul", 20 "paul", 20 "bob", 10
my output
name: bob, score: 30 name: bob, score: 30 name: paul, score: 40 name: paul, score: 40 name: bob, score: 30
so my main question. algorithm can use copy name , total score onto new arrays only once. while keeping in mind due duplicated new size of list smaller old 1 always.
thank in advance.
i want achieve arrays , conditional loops.
create map using name
key , score
value. if find name
exists, retrieve value, add current score
retrieved score
, , update putting new score
map.
after you've completed on initial name[]
, score[]
arrays, iterate on map's entryset
, add each name , score respective new array[]
s.
(by way, has nothing parallel processing or multi-threading.)
Comments
Post a Comment