Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have a array which contains dates with duplications.i want result as dates with out duplication

ex 12/12/2013
12/12/2013
12/12/2013
12/12/2013
01/01/2014
01/02/2014
12/12/2013
11/11/2013
11/11/2013
10/10/2013

result should be
12/12/2013
01/01/2014
01/02/2014
12/12/2013
11/11/2013
10/10/2013

please help me to do this.provide any sample code
Posted
Updated 7-Jan-14 3:05am
v2

 
Share this answer
 
Comments
Maciej Los 7-Jan-14 9:06am    
Nice, +5!
Tejas Vaishnav 7-Jan-14 9:11am    
Thnaks,
HI Kanna,

You could try this below code,
will do the trick for you
C#
string[] dateWithDup =new string[]{ "12/12/2013","12/12/2013","12/12/2013","12/12/2013","01/01/2014","01/02/2014","12/12/2013","11/11/2013","11/11/2013","10/10/2013"};
            string[] dateWithoutDup = new string[50];
            int count = 0;
            foreach (var date in dateWithDup)
            {
                if (!dateWithoutDup.Contains(date))
                {
                    dateWithoutDup[count] = date;
                    count++;
                }
            }

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
Solution 1 by Tejas Vaishnav is very good. I would provide another linq query:
C#
DateTime[] dt = {new DateTime(2013,12,12), new DateTime(2013,12,12),
                new DateTime(2013,12,12), new DateTime(2013,12,12),
                new DateTime(2014,01,01), new DateTime(2014,01,02),
                new DateTime(2013,12,12), new DateTime(2013,11,11),
                new DateTime(2013,11,11), new DateTime(2013,10,10) };

var uniques = dt.GroupBy(g=>g).Where(g=>g.Count()>=1).Select(g=>g.Key);
foreach (DateTime d in uniques)
{
    Console.WriteLine(d.ToString());
}
Console.ReadKey();


Distinct method[^] is - of course - an easiest (and fastest?) way.

To select only duplicates, change Where statement:
C#
Where(g=>g.Count()>1)

To select uniques values (non-duplicates), change Where statement:
C#
Where(g=>g.Count()==1)
 
Share this answer
 

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