方法一:
static void Main(string[] args) { //看到數組的第一反應應該是排序 int[] array = { 2,4,6,2,8,5,8,10}; //去掉數組中重複的項 //先排序 arrayAsc(array); List<int> list = array.ToList(); for (int i = 0; i < list.Count-1; i++) { if(list[i]== list[i+1]) list.RemoveAt(i+1); } for (int i = 0; i < list.Count - 1; i++) { Console.WriteLine(list[i]); } Console.ReadKey(); } /// <summary> /// 排序 /// </summary> /// <param name="array"></param> /// <returns></returns> private static void arrayAsc(int[] array) { int i, j, temp; for (i = 0; i < array.Length-1; i++) { for (j = i+1; j < array.Length; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } }
方法二;
int[] arry_new = { 3, 5, 9, 8, 10, 5, 3 };數組
HashSet<int> set = new HashSet<int>();spa
foreach (int i in arry_new)
{
set.Add(i);
}code
int[] at = set.ToArray();blog
for (int i = 0; i < at.Length - 1; i++)
{
Console.WriteLine(at[i]);
}排序
Console.ReadKey();string