Click here to Skip to main content
15,905,616 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Algorithm to remove duplicates from an array.

Example :
array having duplicate country names Like
(‘India’,’Pakistan’,United states ofAmerica’,’Germany’,’Pakistan’,’England’,’Ireland’,’India’).

(A windows form can be developed to demonstrate this)
Posted
Updated 11-Apr-12 3:33am
v2
Comments
wizardzz 12-Apr-12 11:45am    
Um, this should be simple for anyone who is familiar with logic, or the use of their brain. What have you tried? I feel this is only HW. Goodluck keeping a real job.

Distinct extension method of IEnumerable Interface can be used for this purpose.

C#
string[] Countries = {
    "India", "Pakistan", "United States of America", "Germany",
    "Pakistan", "England", "Ireland", "India"};
var query = Countries.Distinct();
var distinctCountries = query.ToArray();

The IEqualityComparer<T> parameter can be supplied for controlling comparison of strings.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 11-Apr-12 20:43pm    
I only fixed the spelling and capitalization for the 3rd country (pun unintended :-) and formatting of the generic declaration -- you probably forgot to escape angular brackets using HTML character entities &lt; and &gt;. By the way, the menu item "encode" on top of text area helps to do that.

The answer itself got my 5; I think this kind of extension methods is an elegant thing.
After all, who needs yet another India and one more Pakistan? :-)
--SA
VJ Reddy 11-Apr-12 20:57pm    
Thank you very much, SA, for correction and your comment. Thank you for the suggestion regarding HTML character entities.
I suppose this is not your homework.
Any way I wont give direct code, check out the details/options here

http://stackoverflow.com/questions/9673/remove-duplicates-from-array[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-12 20:48pm    
Essentially, exact same thing as the one written by VJ; my 5, too.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900