June 15, 2006

removing duplicates from array

One simple way to do it is sort the array by some efficient algos ie O(N*log(N)).

rather than that you can create a new associative array with indexes as the value of the given array elements .

so to remove duplicate in, say,

A[]={1,3,5,3,8,1,3,5,8,5,5,4}
we create a new associative array B[] such that

for (i=0; i< style="color: rgb(51, 51, 255);"> B[A[i]] = 1;

now you can get the array of keys having value '1' in A
ie
A = getkey(B,1)
where getkey will return the array of keys where value of B[key]=1
so A will not have the duplicates.

No comments: