我有一系列数字,我需要确保它们是唯一的.我在互联网上找到了下面的代码片段,它的工作情况很好,直到数组中的数字为零.我发现这个其他脚本在SO上看起来几乎就像它,但它不会失败.
所以为了帮助我学习,有人可以帮我确定原型脚本出错的地方吗?
Array.prototype.getUnique = function() {
var o = {}, a = [], i, e;
for (i = 0; e = this[i]; i++) {o[e] = 1};
for (e in o) {a.push (e)};
return a;
}
Run Code Online (Sandbox Code Playgroud)
List是否具有可以防止向其添加重复条目的属性或机制,或者每次都必须在列表中搜索它?
如果,它会更好:
List<String> AAppsToDisplay = new List<String>();
AAppsToDisplay.DuplicatesAllowed = false;
...
while (odr.Read())
{
AAppsToDisplay.Add(odr.GetString(0));
}
Run Code Online (Sandbox Code Playgroud)
而不是必须这样做:
List<String> AAppsToDisplay = new List<String>();
. . .
String s = String.Empty;
while (odr.Read())
{
s = odr.GetString(0);
if (! AAppsToDisplay.Contains(s))
{
AAppsToDisplay.Add(s);
}
}
Run Code Online (Sandbox Code Playgroud)