Wednesday, 26 November 2014

How to finding duplicate elements in array

  1. var counts = new Dictionary<int, int>();
  2. int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
  3. for (int i = 0; i < array.Length; i++)
  4. {
  5. int currentVal = array[i];
  6. if (counts.ContainsKey(currentVal))
  7. counts[currentVal]++;
  8. else
  9. counts[currentVal] = 1;
  10. }
  11. foreach (var kvp in counts)
  12. MessageBox.Show("\t\n " + kvp.Key + " occurs " + kvp.Value);

0 comments:

Post a Comment