Finding duplicate integers in an array and display

2019-01-17 15:55发布

I'm working on a code that prints out duplicated integers from an array with the number of their occurrence. I'm not allowed to use LINQ, just a simple code. I think I'm so close but confused about how to get a correct output:

class Program
{
    static void Main(string[] args)
    {              
        int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
        int count = 1;
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = i; j < array.Length - 1 ; j++)
            {

               if(array[j] == array[j+1])
                  count = count + 1;
            }
            Console.WriteLine("\t\n " + array[i] + "occurse" + count);
            Console.ReadKey();
        }
    }
}

17条回答
萌系小妹纸
2楼-- · 2019-01-17 16:07

// This method counts a total number of duplicate elements in an array

public void DuplicateElementsInArray(int[] numbers)
    {
        int count = 0;

        for (int i = 0; i < numbers.Length; i++)
        {

            for (int j = i; j < numbers.Length - 1; j++)
            {
                if (numbers[i] == numbers[j+1])
                {                       
                    count++;
                    break;
                }                       
            }               
        }
        Console.WriteLine("Total number of duplicate elements found in the array is: {0}", count);
    }
查看更多
相关推荐>>
3楼-- · 2019-01-17 16:08

/This is the answer that helps you to find the duplicate integer values using Forloop and it will return only the repeated values apart from its times of occurences/

    public static void Main(string[] args)
    {
        //Array list to store all the duplicate values
        int[] ary = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
        ArrayList dup = new ArrayList();

        for (int i = 0; i < ary.Length; i++)
        {
            for (int j = i + 1; j < ary.Length; j++)
            {
                if (ary[i].Equals(ary[j]))
                {
                    if (!dup.Contains(ary[i]))
                    {
                        dup.Add(ary[i]);
                    }
                }
            }
        }
        Console.WriteLine("The numbers which duplicates are");
        DisplayArray(dup);
    }
    public static void DisplayArray(ArrayList ary)
    {
        //loop through all the elements
        for (int i = 0; i < ary.Count; i++)
        {
            Console.Write(ary[i] + " ");
        }
        Console.WriteLine();
        Console.ReadKey();
    }
查看更多
我只想做你的唯一
4楼-- · 2019-01-17 16:08

public static void Main(string[] args)

{

Int[] array = {10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12};

List<int> doneNumbers = new List<int>();


for (int i = 0; i < array.Length - 1; i++)

{

    if(!doneNumbers.Contains(array[i]))

    {

        int currentNumber = array[i];

        int count = 0;

        for (int j = i; j < array.Length; j++)

        {

            if(currentNumber == array[j])

            {

                count++;

            }

        }

        Console.WriteLine("\t\n " + currentNumber +" "+ " occurs " + " "+count + " "+" times");

        doneNumbers.Add(currentNumber);

        Console.ReadKey();
      }

   }

}

}

}

查看更多
该账号已被封号
5楼-- · 2019-01-17 16:09

Use Group by:

int[] values = new []{1,2,3,4,5,4,4,3};

var groups = values.GroupBy(v => v);
foreach(var group in groups)
    Console.WriteLine("Value {0} has {1} items", group.Key, group.Count());
查看更多
家丑人穷心不美
6楼-- · 2019-01-17 16:10

Just use the code below,no need to use dictionary or ArrayList.Hope this helps :)

public static void Main(string[] args)
{              
    int [] array =new int[]{10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12};
    Array.Sort(array);
    for (int i = 0; i < array.Length; i++)
    {
        int count = 1,k=-1;
        for (int j = i+1; j < array.Length; j++)
        {  

                if(array[i] == array[j])
                {
                    count++;
                    k=j-1;
                }
                else break;
        }
        Console.WriteLine("\t\n " + array[i] + "---------->" + count);            
        if(k!=-1)
            i=k+1;          
    }
}
查看更多
7楼-- · 2019-01-17 16:13
int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 7, 7, 8, 9, 7, 12, 12 };
Dictionary<int, int> duplicateNumbers = new Dictionary<int, int>();
int count=1;
for (int i = 0; i < array.Length; i++)
{
    count=1;
    if(!duplicateNumbers.ContainsKey(array[i]))
    {
        for (int j = i; j < array.Length-1; j++)
        {
            if (array[i] == array[j+1])
            {
                count++;                            
            }
        }
        if (count > 1)
        {
            duplicateNumbers.Add(array[i], count);
        }
    }
}
foreach (var num in duplicateNumbers)
{
    Console.WriteLine("Duplicate numbers, NUMBER-{0}, OCCURRENCE- {1}",num.Key,num.Value);
}
查看更多
登录 后发表回答