如何检查此阵列中的重复答案? C#

时间:2021-03-08 07:29:27

Sorry for the newbie question. Could someone help me out? Simple array here. What's the best/easiest method to check all the user 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 should have added I need to check to happen before the numbers are output. Thanks

我应该补充一下,我需要在输出数字之前检查一下。谢谢

5 个解决方案

#1


15  

One simple approach is via LINQ:

一个简单的方法是通过LINQ:

bool allUnique = Numbers.Distinct().Count() == Numbers.Length;

#2


2  

Another approach is using a HashSet<string>:

另一种方法是使用HashSet

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

or with Enumerable.All:

或者使用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 is more efficient when the sequence is very large since it does not create the set completely but one after each other and will return false as soon as it detects a duplicate.

Enunmerable.All在序列非常大时效率更高,因为它不会完全创建集合,而是一个接一个地创建集合,并且一旦检测到重复就会返回false。

Here's a demo of this effect: http://ideone.com/G48CYv

以下是此效果的演示:http://ideone.com/G48CYv

  • HashSet constructor memory consumption: 50 MB, duration: 00:00:00.2962615
  • HashSet构造函数内存消耗:50 MB,持续时间:00:00:00.2962615
  • Enumerable.All memory consumption: 0 MB, duration: 00:00:00.0004254
  • Enumerable.All内存消耗:0 MB,持续时间:00:00:00.0004254

msdn

MSDN

The HashSet<T> class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

HashSet 类提供高性能的集合操作。集合是一个不包含重复元素的集合,其元素没有特定的顺序。

#3


1  

The easiest way, in my opinion, would be to insert all values inside a set and then check if its size is equal to the array's size. A set can't contain duplicate values, so if any value is duplicate, it won't be inserted into the set.

在我看来,最简单的方法是在集合中插入所有值,然后检查它的大小是否等于数组的大小。集合不能包含重复值,因此如果任何值重复,则不会将其插入集合中。

This is also OK in complexity if you don't have millions of values, because insertion in a set is done in O(logn) time, so total check time will be O(nlogn).

如果你没有数百万的值,这也很复杂,因为插入集合是在O(logn)时间完成的,所以总检查时间将是O(nlogn)。

If you want something optimal in complexity, you can do this in O(n) time by going through the array, and putting each value found into a hash map while incrementing its value: if value doesn't exist in set, you add it with count = 1. If it does exist, you increment its count. Then, you go through the hash map and check that all values have a count of one.

如果你想要复杂度最优的东西,可以在O(n)时间内通过遍历数组,并将每个值找到哈希映射,同时递增其值:如果set中不存在值,则添加它count = 1.如果确实存在,则递增计数。然后,您将浏览哈希映射并检查所有值是否为1。

#4


1  

If you are just trying to make sure that your listbox doesn't have dups then use this:

如果您只是想确保列表框没有重复,请使用以下命令:

if(!lbNumbers.Items.Contains(Result))    
    lbNumbers.Items.Add(Result);

#5


0  

What 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;
}

#1


15  

One simple approach is via LINQ:

一个简单的方法是通过LINQ:

bool allUnique = Numbers.Distinct().Count() == Numbers.Length;

#2


2  

Another approach is using a HashSet<string>:

另一种方法是使用HashSet

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

or with Enumerable.All:

或者使用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 is more efficient when the sequence is very large since it does not create the set completely but one after each other and will return false as soon as it detects a duplicate.

Enunmerable.All在序列非常大时效率更高,因为它不会完全创建集合,而是一个接一个地创建集合,并且一旦检测到重复就会返回false。

Here's a demo of this effect: http://ideone.com/G48CYv

以下是此效果的演示:http://ideone.com/G48CYv

  • HashSet constructor memory consumption: 50 MB, duration: 00:00:00.2962615
  • HashSet构造函数内存消耗:50 MB,持续时间:00:00:00.2962615
  • Enumerable.All memory consumption: 0 MB, duration: 00:00:00.0004254
  • Enumerable.All内存消耗:0 MB,持续时间:00:00:00.0004254

msdn

MSDN

The HashSet<T> class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

HashSet 类提供高性能的集合操作。集合是一个不包含重复元素的集合,其元素没有特定的顺序。

#3


1  

The easiest way, in my opinion, would be to insert all values inside a set and then check if its size is equal to the array's size. A set can't contain duplicate values, so if any value is duplicate, it won't be inserted into the set.

在我看来,最简单的方法是在集合中插入所有值,然后检查它的大小是否等于数组的大小。集合不能包含重复值,因此如果任何值重复,则不会将其插入集合中。

This is also OK in complexity if you don't have millions of values, because insertion in a set is done in O(logn) time, so total check time will be O(nlogn).

如果你没有数百万的值,这也很复杂,因为插入集合是在O(logn)时间完成的,所以总检查时间将是O(nlogn)。

If you want something optimal in complexity, you can do this in O(n) time by going through the array, and putting each value found into a hash map while incrementing its value: if value doesn't exist in set, you add it with count = 1. If it does exist, you increment its count. Then, you go through the hash map and check that all values have a count of one.

如果你想要复杂度最优的东西,可以在O(n)时间内通过遍历数组,并将每个值找到哈希映射,同时递增其值:如果set中不存在值,则添加它count = 1.如果确实存在,则递增计数。然后,您将浏览哈希映射并检查所有值是否为1。

#4


1  

If you are just trying to make sure that your listbox doesn't have dups then use this:

如果您只是想确保列表框没有重复,请使用以下命令:

if(!lbNumbers.Items.Contains(Result))    
    lbNumbers.Items.Add(Result);

#5


0  

What 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;
}