Ok so i have this little problem, i have to find all the elements in an array that are duplicates ex:{1,1,1,2,2,3,4,5,6} 1 repeats it self 3 times and 2 two times things like that this is what i have so far...

static void Main(string[] args)
        {
            int contador = 1,rep=0;
            Console.Write("Tamano del arreglo? ");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine();
            int[] numbers = new int[n];
            for (int i = 0; i <= (n - 1); i++) {
                Console.WriteLine("Ingresar los valores de arreglo "+i);
                numbers[i] = int.Parse(Console.ReadLine());
            }
            for (int i = 0; i <= (n - 1); i++){
                for(int j=0;j<=(i-1);j++){
                       if(numbers[i] == numbers[j]){
                           contador++;
                           rep = numbers[i];
                           }
                    }
            }
            for (int i = 0; i <= (n - 1); i++) {
                Console.WriteLine("numbers["+i+"] "+numbers[i]);
            }
            Console.WriteLine();
            if (contador > 1){
                Console.WriteLine("Se repitio " + contador + " veces el numero " + rep);
            }
        }

Use dictionary collection:

// Dictionary, key is number from the list and the associated value is the number of times the key is found
Dictionary<int, int> occurrences = new Dictionary<int, int>();

int[] test = new int[] { 1, 1, 1, 2, 2, 3, 4, 5, 6 }; // Debug data

// Loop test data
foreach (int value in test)
{
    if (occurrences.ContainsKey(value)) // Check if we have found this key before
    {
        // Key exists. Add number of occurrences for this key by one
        occurrences[value]++;
    }
    else
    {
        // This is a new key so add it. Number 1 indicates that this key has been found one time
        occurrences.Add(value, 1);
    }
}
// Dump result
foreach (int key in occurrences.Keys)
{
    MessageBox.Show("Integer " + key.ToString() + " was found " + occurrences[key].ToString() + " times");
}

You need to import: using System.Collections.Generic; Back to your original question: duplicates are all those keys (values) in the dictionary which have an associated value greater than one.

HTH

...or if you can use LINQ

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_380418
{
   class Program
   {
      static void Main(string[] args)
      {
         new List<int>{1,1,1,2,2,3,4,5,6}
         .ToLookup(k => k)
         .Where(i => !i.Count().Equals(1))
         .ToList().ForEach(i =>
            Console.WriteLine("{0} is duplicated {1} times.", i.Key, i.Count()-1)
         );
      }
   }
}
commented: Deep knowledge! I'm still learning LINQ. +14

The best way would be to use a dictionary collection. Key will be an actual number, and Value it`s repetition.

This is the code that it does what I explained in the post abouve:

int[] numsArr = { 1, 1, 1, 2, 2, 3, 4, 5, 6 };
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < numsArr.Length; i++)
            {
                if (dic.ContainsKey(numsArr[i]))
                {
                    dic[numsArr[i]] = ++dic[numsArr[i]];
                }
                else
                    dic.Add(numsArr[i], 1);
            }

            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<int, int> kvp in dic)
                sb.AppendLine(String.Format("Number {0} has {1} repetitions.", kvp.Key, kvp.Value));
            Console.WriteLine(sb.ToString());
            Console.ReadLine();

...but since it's comping from user input, you might want to validate actual integers.

using System;
using System.Collections.Generic;

namespace DW_380418_2
{
   class Program
   {
      static void Main(string[] args)
      {
         int n = 0;

         do
         {
            Console.Write("Tamano del arreglo? ");
         } while (!int.TryParse(Console.ReadLine().Trim(), out n));

         Console.WriteLine();

         Dictionary<int, int> map_i2iNumbers = new Dictionary<int, int>(n);
         int iNewNum = 0;
         for (int i = 0; i < n; i++)
         {
            do
            {
               Console.Write("Ingresar los valores de arreglo {0}: ", (i + 1));
            } while (!int.TryParse(Console.ReadLine().Trim(), out iNewNum));
            
            if (map_i2iNumbers.ContainsKey(iNewNum))
            {
               map_i2iNumbers[iNewNum]++;
               continue;
            }

            map_i2iNumbers.Add(iNewNum, 0);
         }

         foreach (int i in map_i2iNumbers.Keys)
         {
            Console.WriteLine(
               "El numero {0} se repitio {1} ve{2}.", i,
               map_i2iNumbers[i],
               map_i2iNumbers[i].Equals(1) ? "z" : "ces"
               );
         }
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.