//输出重复的元素
public static void ArrayDuplicate(int[] array1)
{
//string[] array1 = new string[] { "7ss", "4s", "7g", "1a","4g", "1e", "5e", "2a", "5e" };
ArrayList list1 = new ArrayList(); //储存去重后剩余的元素
ArrayList list2 = new ArrayList(); //储存重复的元素
for (int i = 0; i < array1.Length; i++)
{
if (!list1.Contains(array1[i]))
list1.Add(array1[i]); //添加第一次出现的元素
else
list2.Add(array1[i]); //添加重复的元素
}
foreach (int i in list1)
{
Console.WriteLine(i);
}
Console.WriteLine("duplicate");
foreach (int i in list2)
{
Console.WriteLine(i);
}
}
//输出不重复的元素
public static void ArrayNoDuplicate(int[] array1)
{
//string[] array1 = new string[] { "7ss", "4s", "7g", "1a","4g", "1e", "5e", "2a", "5e" };
ArrayList list1 = new ArrayList(); //储存去重后剩余的元素
ArrayList list2 = new ArrayList(); //储存重复的元素
for (int i = 0; i < array1.Length; i++)
{
if (!list1.Contains(array1[i]))
list1.Add(array1[i]); //添加第一次出现的元素
else
{
list1.Remove(array1[i]);
list2.Add(array1[i]); //添加重复的元素
}
}
foreach (int i in list1)
{
Console.WriteLine(i);
}
Console.WriteLine("duplicate");
foreach (int i in list2)
{
Console.WriteLine(i);
}
}