.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Remove duplicate items from array

Posted By:satyapriyanayak       Posted Date: February 27, 2014    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

In this Code Snippets we will know how to remove duplicate items from array
 

Method-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Remove_duplicate
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Original Names");

            string[] strs = new string[4] { "Raj", "Ravi", "Ravi", "Rahul" };

            for (int i = 0; i < strs.Length; i++)
            {

                Console.WriteLine(strs[i]);

            }

            Console.WriteLine("Distinct Names");

            var Results = strs.Distinct();

            foreach (var item in Results)
            {

                Console.WriteLine(item);

            }

            Console.ReadLine();

            
        }
    }
}

Method-2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Remove_duplicate
{
    class Program
    {
        static void Main(string[] args)
        {
           

            String[] myString = new String[] { "Raj", "Raj", "Ravi", "Ravi", "Rahul" };

            IEnumerable<String> disctinctName = myString.Distinct();

            foreach (String theString in disctinctName)
            {

                Console.WriteLine(theString);

            }

            Console.ReadLine();
        }
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend