如何检查数组中的重复项,然后对其值执行某些操作?

时间:2021-11-27 07:40:59

I have an array for example("1:2","5:90","7:12",1:70,"29:60") Wherein ID and Qty are separated by a ':' (colon), what I want to do is when there's a duplicate of IDs the program will add the qty and return the new set of arrays so in the example it will become ("1:72","5:90","7:12","29:60").

我有一个数组例如(“1:2”,“5:90”,“7:12”,1:70,“29:60”)其中ID和Qty由':'(冒号)分隔,我想要做的是当有重复的ID时,程序将添加数量并返回新的数组,因此在示例中它将变为(“1:72”,“5:90”,“7:12” , “29:60”)。

Ex.2 ("1:2","5:90","7:12","1:70","29:60","1:5") becomes ("1:77","5:90","7:12","29:60").

Ex.2(“1:2”,“5:90”,“7:12”,“1:70”,“29:60”,“1:5”)变为(“1:77”,“5 :90" , “7:12”, “29:60”)。

I want to solve it without using linq.

我想在不使用linq的情况下解决它。

6 个解决方案

#1


1  

If you want it without LINQ...

如果你想要没有LINQ ......

var totalQuantities = new Dictionary<int, int>();
foreach(var raw in sourceArr) {
    var splitted = raw.Split(':');
    int id = int.Parse(splitted[0]);
    int qty = int.Parse(splitted[1]);
    if(!totalQuantities.ContainsKey(id)) {
        totalQuantities[id] = 0;
    }
    totalQuantities[id] += qty;
}

var result = new string[totalQuantities.Count];
int i=0;
foreach(var kvp in totalQuantities) {
    result[i] = string.Format("{0}:{1}", kvp.Key, kvp.Value);
    i++;
}

#2


3  

var foo = array.Select(s => s.Split(':'))
               .GroupBy(x => x[0])
               .Select(g => 
                    String.Format(
                        "{0}:{1}",
                        g.Key,
                        g.Sum(x => Int32.Parse(x[1]))
                    )
               )
               .ToArray();

Note, it's not necessary to parse the "keys," only the values.

注意,没有必要解析“键”,只解析值。

Without LINQ:

没有LINQ:

var dictionary = new Dictionary<string, int>();
foreach (var group in array) {
    var fields = group.Split(':');
    if (!dictionary.ContainsKey(fields[0])) {
        dictionary.Add(fields[0], 0);
    }
    dictionary[fields[0]] += Int32.Parse(fields[1]);
}
string[] foo = new string[dictionary.Count];
int index = 0;
foreach (var kvp in dictionary) {
    foo[index++] = String.Format("{0}:{1}", kvp.Key, kvp.Value);
}

#3


2  

You have to do this manually. Loop through each list, check the ID for each element. Put it in a Dictionary<int, int>, Dictionary<id, qt>. If the dictionary contains the id, add it to the value.

您必须手动执行此操作。遍历每个列表,检查每个元素的ID。把它放在Dictionary ,Dictionary 中。如果字典包含id,请将其添加到值中。 ,qt> ,int>

  1. Loop, add, check using Dictionary class.
  2. 使用Dictionary类循环,添加,检查。

#4


1  

(
    from raw in arr
        let splitted = raw.Split(':')
        let id = int.Parse(splitted[0])
        let qty = int.Parse(splitted[1])
        let data = new { id, qty }
        group data by data.id into grp
            let totalQty = grp.Sum(val => val.qty)
            let newStr = string.Format("{0}:{1}", grp.Key, totalQty
            select newStr
)
.ToArray()

Note that the code may contain accidental errors, as it was written in notepad.

请注意,代码可能包含意外错误,因为它是用记事本编写的。

#5


1  

var input=new string[]{"1:2","5:90","7:12","1:70","29:60","1:5"};
var result=input
             .Select(s=>s.Split(':'))
             .Select(x=>x.Select(s=>int.Parse(s)).ToArray())
             .GroupBy(x=>x[0])
             .Select(g=>g.Key+":"+g.Sum(x=>x[1]));

I was too lazy to specify the culture everywhere. You probably want to do that before putting it into production, or it will fail for cultures with unusual integer representations.

我懒得到处都指明文化。您可能希望在将其投入生产之前执行此操作,否则对于具有异常整数表示的文化将失败。

var totals=new Dictionary<int,int>
foreach(string s in input)
{
  string[] parts=s.Split(':');
  int id=int.Parse(parts[0]);
  int quantity=int.Parse(parts[0]);
  int totalQuantity;
  if(!totals.TryGetValue(id,out totalQuantity))
      totalQuantity=0;//Yes I know this is redundant
  totalQuanity+=quantity;
  totals[id]=totalQuantity;
}
var result=new List<string>();
foreach(var pair in totals)
{
  result.Add(pair.Key+":"+pair.Value);
}

#6


1  

try this:

尝试这个:

List<string> items = new List<string>(new string[] { "1:2", "5:90", "7:12", "1:70", "29:60" });
Dictionary<string, int> dictionary = new Dictionary<string, int>();

foreach (string item in items)
{
    string[] data = item.Split(':');
    string key = data[0];

    if (!dictionary.ContainsKey(data[0]))
    {
        int value = dictionary[data[0]];
        dictionary[key] += int.Parse(data[1]);
    }
}

//Used dictionary values here

#1


1  

If you want it without LINQ...

如果你想要没有LINQ ......

var totalQuantities = new Dictionary<int, int>();
foreach(var raw in sourceArr) {
    var splitted = raw.Split(':');
    int id = int.Parse(splitted[0]);
    int qty = int.Parse(splitted[1]);
    if(!totalQuantities.ContainsKey(id)) {
        totalQuantities[id] = 0;
    }
    totalQuantities[id] += qty;
}

var result = new string[totalQuantities.Count];
int i=0;
foreach(var kvp in totalQuantities) {
    result[i] = string.Format("{0}:{1}", kvp.Key, kvp.Value);
    i++;
}

#2


3  

var foo = array.Select(s => s.Split(':'))
               .GroupBy(x => x[0])
               .Select(g => 
                    String.Format(
                        "{0}:{1}",
                        g.Key,
                        g.Sum(x => Int32.Parse(x[1]))
                    )
               )
               .ToArray();

Note, it's not necessary to parse the "keys," only the values.

注意,没有必要解析“键”,只解析值。

Without LINQ:

没有LINQ:

var dictionary = new Dictionary<string, int>();
foreach (var group in array) {
    var fields = group.Split(':');
    if (!dictionary.ContainsKey(fields[0])) {
        dictionary.Add(fields[0], 0);
    }
    dictionary[fields[0]] += Int32.Parse(fields[1]);
}
string[] foo = new string[dictionary.Count];
int index = 0;
foreach (var kvp in dictionary) {
    foo[index++] = String.Format("{0}:{1}", kvp.Key, kvp.Value);
}

#3


2  

You have to do this manually. Loop through each list, check the ID for each element. Put it in a Dictionary<int, int>, Dictionary<id, qt>. If the dictionary contains the id, add it to the value.

您必须手动执行此操作。遍历每个列表,检查每个元素的ID。把它放在Dictionary ,Dictionary 中。如果字典包含id,请将其添加到值中。 ,qt> ,int>

  1. Loop, add, check using Dictionary class.
  2. 使用Dictionary类循环,添加,检查。

#4


1  

(
    from raw in arr
        let splitted = raw.Split(':')
        let id = int.Parse(splitted[0])
        let qty = int.Parse(splitted[1])
        let data = new { id, qty }
        group data by data.id into grp
            let totalQty = grp.Sum(val => val.qty)
            let newStr = string.Format("{0}:{1}", grp.Key, totalQty
            select newStr
)
.ToArray()

Note that the code may contain accidental errors, as it was written in notepad.

请注意,代码可能包含意外错误,因为它是用记事本编写的。

#5


1  

var input=new string[]{"1:2","5:90","7:12","1:70","29:60","1:5"};
var result=input
             .Select(s=>s.Split(':'))
             .Select(x=>x.Select(s=>int.Parse(s)).ToArray())
             .GroupBy(x=>x[0])
             .Select(g=>g.Key+":"+g.Sum(x=>x[1]));

I was too lazy to specify the culture everywhere. You probably want to do that before putting it into production, or it will fail for cultures with unusual integer representations.

我懒得到处都指明文化。您可能希望在将其投入生产之前执行此操作,否则对于具有异常整数表示的文化将失败。

var totals=new Dictionary<int,int>
foreach(string s in input)
{
  string[] parts=s.Split(':');
  int id=int.Parse(parts[0]);
  int quantity=int.Parse(parts[0]);
  int totalQuantity;
  if(!totals.TryGetValue(id,out totalQuantity))
      totalQuantity=0;//Yes I know this is redundant
  totalQuanity+=quantity;
  totals[id]=totalQuantity;
}
var result=new List<string>();
foreach(var pair in totals)
{
  result.Add(pair.Key+":"+pair.Value);
}

#6


1  

try this:

尝试这个:

List<string> items = new List<string>(new string[] { "1:2", "5:90", "7:12", "1:70", "29:60" });
Dictionary<string, int> dictionary = new Dictionary<string, int>();

foreach (string item in items)
{
    string[] data = item.Split(':');
    string key = data[0];

    if (!dictionary.ContainsKey(data[0]))
    {
        int value = dictionary[data[0]];
        dictionary[key] += int.Parse(data[1]);
    }
}

//Used dictionary values here