Listdagi dublikatlardan getCount

Ushbu kod bor:

public List Duplicates(List sequence)
{

    int[] countArr = new int[156];
    foreach (int i in sequence)
    {
        countArr[i]++;
    }

    List resultList = new List();

    for (var i = 0; i < countArr.Length; i++)
    {
        if (countArr[i] > 1)
        {
            resultList.Add(i);
        }
    }

    return resultList;
}

Bu menga takrorlanadigan elementlarni oladi, lekin bu elementlar necha marta takrorlanmasin.

Berilgan har qanday yordam uchun oldindan rahmat.

EDIT -ni tanlang

LINQ'dan foydalanishni xohlamayman

0
resultList.Count harakat qilib ko'ring
qo'shib qo'ydi muallif Saurabh Srivastava, manba

7 javoblar

That's a very complicated way you use, i'd rather return a Dictionary:

public static Dictionary Duplicates(IEnumerable sequence)
{
    var duplicates = new Dictionary();
    foreach (int i in sequence)
    {
        if(duplicates.ContainsKey(i))
            duplicates[i]++;
        else
            duplicates.Add(i, 1);
    }
    return duplicates;
}
2
qo'shib qo'ydi
Men ContainsKey dan foydalanishim mumkin emas. Mening kodimdan foydalangan aminmisiz?
qo'shib qo'ydi muallif Tim Schmelter, manba
Men berilgan har bir javobni bajarishga harakat qilaman. Lekin bu bilan men quyidagi xatolarni olaman: KeyNotFoundException: Berilgan kalit lug'atda mavjud emas edi. System.Collections.Generic.Dictionary `2 [System.Int32, tizim. & Zwnj; Int32] .get_Item (Int32 kaliti)
qo'shib qo'ydi muallif NachoMiguel, manba

That's a very complicated way you use, i'd rather return a Dictionary:

public static Dictionary Duplicates(IEnumerable sequence)
{
    var duplicates = new Dictionary();
    foreach (int i in sequence)
    {
        if(duplicates.ContainsKey(i))
            duplicates[i]++;
        else
            duplicates.Add(i, 1);
    }
    return duplicates;
}
2
qo'shib qo'ydi
Men ContainsKey dan foydalanishim mumkin emas. Mening kodimdan foydalangan aminmisiz?
qo'shib qo'ydi muallif Tim Schmelter, manba
Men berilgan har bir javobni bajarishga harakat qilaman. Lekin bu bilan men quyidagi xatolarni olaman: KeyNotFoundException: Berilgan kalit lug'atda mavjud emas edi. System.Collections.Generic.Dictionary `2 [System.Int32, tizim. & Zwnj; Int32] .get_Item (Int32 kaliti)
qo'shib qo'ydi muallif NachoMiguel, manba

That's a very complicated way you use, i'd rather return a Dictionary:

public static Dictionary Duplicates(IEnumerable sequence)
{
    var duplicates = new Dictionary();
    foreach (int i in sequence)
    {
        if(duplicates.ContainsKey(i))
            duplicates[i]++;
        else
            duplicates.Add(i, 1);
    }
    return duplicates;
}
2
qo'shib qo'ydi
Men ContainsKey dan foydalanishim mumkin emas. Mening kodimdan foydalangan aminmisiz?
qo'shib qo'ydi muallif Tim Schmelter, manba
Men berilgan har bir javobni bajarishga harakat qilaman. Lekin bu bilan men quyidagi xatolarni olaman: KeyNotFoundException: Berilgan kalit lug'atda mavjud emas edi. System.Collections.Generic.Dictionary `2 [System.Int32, tizim. & Zwnj; Int32] .get_Item (Int32 kaliti)
qo'shib qo'ydi muallif NachoMiguel, manba

GroupBy foydalaning:

sequence.GroupBy(i => i).Select(g => new {Value = g.Key, Amount = g.Count()})

Agar siz Linq (nima uchun ???) dan foydalanishni istamasangiz, unda bir nechta qiymati va summani to'plang:

List> resultList = new List>();

for (var i = 0; i < countArr.Length; i++)
{
    if (countArr[i] > 1)
    {
        resultList.Add(Tuple.Create(i, countArr[i]));
    }
}
2
qo'shib qo'ydi
"LINQ" ni ishlatishni xohlamayman "
qo'shib qo'ydi muallif Tim Schmelter, manba
@TimSchmelter Ha, javobni yozganimda tahrir qilmadim.
qo'shib qo'ydi muallif Maksim Simkin, manba

GroupBy foydalaning:

sequence.GroupBy(i => i).Select(g => new {Value = g.Key, Amount = g.Count()})

Agar siz Linq (nima uchun ???) dan foydalanishni istamasangiz, unda bir nechta qiymati va summani to'plang:

List> resultList = new List>();

for (var i = 0; i < countArr.Length; i++)
{
    if (countArr[i] > 1)
    {
        resultList.Add(Tuple.Create(i, countArr[i]));
    }
}
2
qo'shib qo'ydi
"LINQ" ni ishlatishni xohlamayman "
qo'shib qo'ydi muallif Tim Schmelter, manba
@TimSchmelter Ha, javobni yozganimda tahrir qilmadim.
qo'shib qo'ydi muallif Maksim Simkin, manba

Your algorithm already produces the required counts, so all you need to do is to arrange returning them to the caller in some way. One approach is to change the return type to IList>. The collection of pairs you return would contain the number in the Key property, and its count in the Value property:

IList> Duplicates(List sequence) {
    var countArr = new int[156];
    foreach (int i in sequence) {
        countArr[i]++;
    }
    var resultList = new List>();
    for (var i = 0; i < countArr.Length; i++) {
        if (countArr[i] > 1) {
            resultList.Add(new KeyValuePair(i, countArr[i]));
        }
    }
    return resultList;
}
1
qo'shib qo'ydi

Lug'at bilan oddiy javob:

void Main()
{
    List intlist = new List
    {
        1,
        1,
        1,
        2,
        2,
        3,
        4,
        4,
        4,
        4
    };


    var dict = new Dictionary();
    foreach (var item in intlist)
    {
        if (!dict.ContainsKey(item))//this checks for the existance of an item
        {
            dict.Add(item, 0);//this initialises the item in the dictionary
        }
        dict[item]++;//this will update the count of the item
    }

   //this is just for linqpad debug output and shows each value and their count
   //this can be achieved with foreach
    dict.Select(x => new { x.Key, x.Value}).Dump();
}

Debug output

Ha, men pastda Tanlash ekanligini bilaman, lekin bu ikki nusxadagi to'plam bilan aloqasi yo'q.

0
qo'shib qo'ydi