To Remove Duplicates From List can be done by many different ways. You can use Distinct extension method to remove duplicates.
Below program that removes duplicates from the list in C#.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List having duplicate integer elements.
List<int> list = new List<int>();
list.Add(7);
list.Add(2);
list.Add(5);
list.Add(5);
list.Add(2);
list.Add(3);
list.Add(7);
// Get distinct elements and convert again into a list.
List<int> distinct = list.Distinct().ToList();
foreach (int value in distinct)
{
Console.WriteLine("Distinct : {0}", value);
}
}
}
OR
You can use LINQ to object to remove duplicates from list,
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List having duplicate string elements.
List<string> list = new List<string>();
list.Add("A");
list.Add("A");
list.Add("C");
list.Add("D");
list.Add("B");
list.Add("A");
list.Add("E");
list.Add("E");
// Get distinct elements.
var distinct = (from item in list orderby item select item).Distinct();
foreach (string value in distinct)
{
Console.WriteLine("Distinct : {0}", value);
}
}
}
The .NET Framework allows developers to use the same set of skills to rapidly buid great applications for the web, windows, services and more.
Sort Integer and String Array in C#
Example 1: Sort Int[] Array
This example int[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a integer array in place. The result is an ascending order of numbers.
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main(string[] args)
{
int[] data = { 999,9,19,555,5,18,399};
Array.Sort(data);
foreach (int i in data)
Console.WriteLine(i);
Console.ReadLine();
}
}
Example 2: Sort string[] Array
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort.
using System;
class Program
{
static void Main()
{
string[] str = new string[]
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
Array.Sort(str);
foreach (string s in str)
Console.WriteLine(s);
Console.ReadLine();
}
Example 3: Sort string Array with LINQ
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort. We will use LINQ query expression to order its contents.
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] str = new string[]
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
var sort = from s in str
orderby s
select s;
foreach (string c in sort)
Console.WriteLine(c);
Console.ReadLine();
}
}
Example 4: Sort string Array in reverse using LINQ
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical decending sort. We will use LINQ query expression to order its contents.
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] str = new string[]
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
var desc = from s in str
orderby s descending
select s;
foreach (string s in desc)
Console.WriteLine(s);
Console.ReadLine();
}
}
Example 5: Sort List
List<string> is a generic List collection of strings. It is stored as a string array. We can use LINQ exact same way we used in Example 3 and 4.
using System;
using System.Collections.Generic;class Program
{
static void Main()
{
List<string> lst = new List<string>()
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
lst.Sort();
foreach (string s in lst)
Console.WriteLine(s);
Console.ReadLine();
}
}
This example int[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a integer array in place. The result is an ascending order of numbers.
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main(string[] args)
{
int[] data = { 999,9,19,555,5,18,399};
Array.Sort(data);
foreach (int i in data)
Console.WriteLine(i);
Console.ReadLine();
}
}
Example 2: Sort string[] Array
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort.
using System;
class Program
{
static void Main()
{
string[] str = new string[]
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
Array.Sort(str);
foreach (string s in str)
Console.WriteLine(s);
Console.ReadLine();
}
Example 3: Sort string Array with LINQ
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort. We will use LINQ query expression to order its contents.
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] str = new string[]
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
var sort = from s in str
orderby s
select s;
foreach (string c in sort)
Console.WriteLine(c);
Console.ReadLine();
}
}
Example 4: Sort string Array in reverse using LINQ
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical decending sort. We will use LINQ query expression to order its contents.
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] str = new string[]
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
var desc = from s in str
orderby s descending
select s;
foreach (string s in desc)
Console.WriteLine(s);
Console.ReadLine();
}
}
Example 5: Sort List
List<string> is a generic List collection of strings. It is stored as a string array. We can use LINQ exact same way we used in Example 3 and 4.
using System;
using System.Collections.Generic;class Program
{
static void Main()
{
List<string> lst = new List<string>()
{
"British",
"Indian",
"American",
"Chinese",
"Malaysians"
};
lst.Sort();
foreach (string s in lst)
Console.WriteLine(s);
Console.ReadLine();
}
}
Subscribe to:
Posts (Atom)