简体   繁体   English

从arraylist中删除包含double数组的重复项

[英]Removing duplicates from arraylist which contain array of double

I have a list which has double array whose items are geocordinates,this list has duplicate elements which i need to remove to preserve only unique values 我有一个具有双精度数组的列表,其项是地理坐标,此列表具有重复的元素,我需要删除这些元素以仅保留唯一值

this is what i have tried 这就是我尝试过的

IList<double[]> result = new List<double[]>(); /list declaration

// result gets value from a soap call 

for (int i = 0; i < result.Count; i++)
{
    for (int j = 0; j < result.Count; j++)
    {
         if (result[i][0].ToString() == result[j][0].ToString() || result[i][1].ToString() == result[j][1].ToString())
         {
             result.Remove(result[j]);
         }
    }
}  

result - my list which has redundant arrays 结果-我的列表中有多余的数组

basically, i need to remove all the arrays inside the list which has same values(x and y geocordinates) 基本上,我需要删除列表中具有相同值(x和y地理坐标)的所有数组

still i have some elements in the list which gets duplicated, can anyone improve my solution please ? 我仍然在列表中有一些元素被重复,有人可以改善我的解决方案吗? would be great help 会很大的帮助

This example takes the 100 in data down to 41 in dataUnique 本示例将data的100减少到dataUnique 41

Random r = new Random(99);
var data = new List<Tuple<decimal, decimal>>();
for (int i = 0; i < 100; i++)
{
    data.Add(new Tuple<decimal, decimal>(r.Next(7)/100m, r.Next(7)/100m));
}
var dataUnique = data.Distinct().ToList();

Wrt your code: Do note that comparing float or double will not work well if any computation has been used on those numbers as binary numbers do not allow the precision needed to do the comparisons.. - Do replace the double by decimal as a first improvement.. WRT代码:请注意,比较floatdouble ,如果任何计算已被用于对这些数字的二进制数不允许做比较,所需要的精度将无法正常工作。 -不要更换doubledecimal作为第一改进..

Using ToString() may or may not help overcome the issue; 使用ToString()可能会也可能无法解决该问题; best not to rely on it.. 最好不要依靠它。

Try this: 尝试这个:

result = result.GroupBy(r => new { val1 = r[0], val2 = r[1] })
               .Select(g => new double[] { g.Key.val1, g.Key.val2 }).ToList();
IList<double[]> result = new List<double[]>(); /list declaration

// result gets value from a soap call 

for (int i = 0; i < result.Count; i++)
{
    for (int j = i + 1; j < result.Count; j++)
    {
         if (result[i][0].ToString() == result[j][0].ToString() && result[i][1].ToString() == result[j][1].ToString())
         {
             result.Remove(result[j]);
             j--;
         }
    }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM