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
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
...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)
);
}
}
}
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
The best way would be to use a dictionary collection. Key will be an actual number, and Value it`s repetition.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
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();
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
...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"
);
}
}
}
}
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402