How to check for duplicate answers in this array? FROM#

Sorry for the newbie question. Can anyone help me out? A simple array is here. What is the best / easiest way to check all input is unique and not duplicated? Thanks

private void btnNext_Click(object sender, EventArgs e) { string[] Numbers = new string[5]; Numbers[0] = txtNumber1.Text; Numbers[1] = txtNumber2.Text; Numbers[2] = txtNumber3.Text; Numbers[3] = txtNumber4.Text; Numbers[4] = txtNumber5.Text; foreach (string Result in Numbers) { lbNumbers.Items.Add(Result); } txtNumber1.Clear(); txtNumber2.Clear(); txtNumber3.Clear(); txtNumber4.Clear(); txtNumber5.Clear(); } } 

}

I had to add what I need to check to happen before the output of numbers. Thanks

+8
arrays c # duplicates
source share
5 answers

One simple approach is through LINQ:

 bool allUnique = Numbers.Distinct().Count() == Numbers.Length; 
+15
source share

Another approach is to use a HashSet<string> :

 var set = new HashSet<string>(Numbers); if (set.Count == Numbers.Count) { // all unique } 

or using Enumerable.All :

 var set = new HashSet<string>(); // HashSet.Add returns a bool if the item was added because it was unique bool allUnique = Numbers.All(text=> set.Add(text)); 

Enunmerable.All more efficient when the sequence is very large, since it does not create the set completely, but one by one and returns false as soon as it detects a duplicate.

Here is a demonstration of this effect: http://ideone.com/G48CYv

  • HashSet constructor memory consumption: 50 MB, duration: 00: 00: 00.2962615
  • Enumerable.All memory consumption: 0 MB, duration: 00: 00: 00.0004254

MSDN

The HashSet<T> class provides high-performance dialing operations. A collection is a collection that does not contain duplicate elements and whose elements do not have a special order.

+2
source share

The easiest way, in my opinion, is to insert all the values โ€‹โ€‹inside the set and then check if its size is equal to its size . A set cannot contain duplicate values, so if any value is duplicated, it will not be inserted into the set.

This is also normal if you do not have millions of values, because the insertion into the set is done in O(logn) time, so the total validation time will be O(nlogn) .

If you want something optimal in complexity, you can do it O(n) times by going through the array and putting each value in the hash map , increasing its value: if the value doesn't exist in the set, you add it with count = 1. If it exists, you increase its number. Then you look at the hash map and verify that all values โ€‹โ€‹have a counter .

+1
source share

If you're just trying to make sure your list has no duplicates, use this:

 if(!lbNumbers.Items.Contains(Result)) lbNumbers.Items.Add(Result); 
+1
source share

How about this:

 public bool arrayContainsDuplicates(string[] array) { for (int i = 0; i < array.Length - 2; i++) { for (int j = i + 1; j < array.Length - 1; j++) { if (array[i] == array[j]) return true; } } return false; } 
0
source share

All Articles