Duplicate elements
How can I remove duplicate elements from an array?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 13, 2012, 10:02 PM EST
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();
}
}
}
Thanks
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 13, 2012, 10:07 PM EST
Another way of doing it,
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
foreach (String theString in disctinctName)
{
Console.WriteLine(theString);
}
Console.ReadLine();
}
}
}
Thanks