Hi,

What could be the most efficient algorithm(time/memory wise) for the below problem?

Q: create an array of unique elements from a given array?

Thanks in advance.

The following can be done in Linear time: (By using a method of hashing)

for each value in array {
        if (!visited(element)) {
               /* store element in new array */ 
               visited[element] = visited /* ie mark as visited */
        }
}

However, it is not space-wise efficient. And, if the elements are strings, it might become hard to define a hash function.

PS: Can anyone share ideas on how to write a hash function in case of strings, in the above problem?

say elems = {"hello", "hey"};

Is it possible to write a hash table with such strings?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.