Programming Languages
C
Subjective
Mar 15, 2013
If there are some duplicate entries in an array and you want to remove them. How would you do that?
Detailed Explanation
If duplicates need to be removed, the best way is to use a hash.
Eg:
sub uniqueentr {
return keys %{{ map { $_ => 1 } @_ }};
}
@array1 = ("tea","coffee","tea","cola”,"coffee");
print join(" ", @array1), "\n";
print join(" ", uniqueentr(@array1)), "\n";
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts