Remove duplicate from array

Satyapriyanayak
Posted by Satyapriyanayak under C# category on | Points: 40 | Views : 1018
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();
}
}
}

Comments or Responses

Login to post response