Remove duplicates from two dimensional array through 3 indexes

[“userId”, “first”, “last”, “priority”, “groupId”],
[“12345”, “John”, “Doe”, “5”, “group1”],
[“12345”, “John”, “Doe”, “7”, “group1”],
[“12345”, “John”, “Doe”, “1”, “group1”],
[“123456”, “Jane”, “Doe”, “5”, “group2”],
[“123456”, “John”, “Doe”, “5”, “group3”],
[“1857”, “Mike”, “Howard”, “5”, “group4”],
[“1857”, “Mike”, “Howard”, “9”, “group4”],
[“12345”, “Bob”, “Forbes”, “1”, “group5”]

If two user ID’s belong to the same group, only keep the array with the highest priority. I’ve tried nested for loops but I am still stuck. The typical array duplicate solution wouldn’t work because joining the array doesn’t make it unique. Any pointers?

I don’t understand. Is userId unique? You have two names with the same id:

[“123456”, “Jane”, “Doe”, “5”, “group2”],
[“123456”, “John”, “Doe”, “5”, “group3”],

Also, why is this an array or arrays and not an array of objects? An object would be a much better fit for user data.

This is a case of a data dump, which comes in this nested array format. I thought about turning it into an object first but in my opinion it might be more complicated. For your question about userID it will be unique once the duplicates are taken care of. There can be only one unique UserId per group, and that’s the userId with the highest priority.

But how can you have two users with the same user ID? How do you know which one is the right one. Are the user ID’s only unique within the groups? That is very confusing.

Personally I would map these to objects, something that makes more sense.

Yes, user ID’s are unique within the group. I just have to make sure I’m saving the user with the highest priority when there is duplicates.

I would probably first split the main array into groups. A new array or object for each group adding each of the nested arrays to the corresponding group array/object (hashmap, etc), but check if an array with that id already exists within that group array/object. If it does then compare the 2 against each other and save the higher priority to the group, thus discarding the other. Then once this is complete, if needed, you can merge the arrays back together.

Thank you I was able to accomplish it after separating by group, then storing them into an object with userId as key, and the entire array as value. I iterated through the array by group, and either add to object if non-existent, or replace the existing if priority is higher, or continue iterating through the loop.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.