What is the best way to check if an multi-dimension array contains 2 or more equal value and only return the first one .
ex : i have an array of people that contain
[0][firstName] = 'John';
[0][lastName] = 'Doe';
[N][firstName] = 'Marco';
[N][lastName] = 'Polo';
[120][firstName] = 'John';
[120][lastName] = 'Doe';
Should detect that the index 120 is a duplicate and remove it .
I’m looking for the best performance , i don’t want to loop on the array and check every time if i have the value or not .
Is there something faster ?
It is the element distinctness problem which is basically
O(NlogN)via sorting and iterating (after sorting, duplicates will be adjacent to each other – so easy to detect them)However, it can be done also in
O(N)on average and withO(N)additional space by storing all elements to a hash table while iterating, and breaking if an element already exists.You might also want to store the original index of each element if you will later need it (and don’t use the index as key).
pseudo code: