Skip to main content
deleted 62 characters in body
Source Link
Ivan Yurchenko
  • 3.8k
  • 1
  • 25
  • 35

As others mentioned, the correct approach here would be to use the HashSet class.

        var hashSet = new HashSet<string>();

        foreach (var obj in testList)
        {
            if (!hashSet.Add(obj.TestValue))
            {
                obj.IsDuplicate = true;
            }
        }

When you add a value first time to the HashSet, it adds successfully and HashSet.Add() method returns true so you don't make any changes to the item. When you're trying to add it second time, HashSet.Add() returns false and you mark your item as a duplicate.

The list will have the following state after finishing running our marking duplicates method:

Matt
Bob
Alice
Claire
Matt DUPLICATE

As others mentioned, the correct approach here would be to use the HashSet class.

        var hashSet = new HashSet<string>();

        foreach (var obj in testList)
        {
            if (!hashSet.Add(obj.TestValue))
            {
                obj.IsDuplicate = true;
            }
        }

When you add a value first time to the HashSet, it adds successfully and HashSet.Add() method returns true so you don't make any changes to the item. When you're trying to add it second time, HashSet.Add() returns false and you mark your item as a duplicate.

The list will have the following state after finishing running our marking duplicates method:

Matt
Bob
Alice
Claire
Matt DUPLICATE

As others mentioned, the correct approach here would be to use the HashSet class.

var hashSet = new HashSet<string>();

foreach (var obj in testList)
{
    if (!hashSet.Add(obj.TestValue))
    {
        obj.IsDuplicate = true;
    }
}

When you add a value first time to the HashSet, it adds successfully and HashSet.Add() method returns true so you don't make any changes to the item. When you're trying to add it second time, HashSet.Add() returns false and you mark your item as a duplicate.

The list will have the following state after finishing running our marking duplicates method:

Matt
Bob
Alice
Claire
Matt DUPLICATE
added 161 characters in body
Source Link
Ivan Yurchenko
  • 3.8k
  • 1
  • 25
  • 35

As others mentioned, the correct approach here would be to use the HashSet class.

        var hashSet = new HashSet<string>();

        foreach (var obj in testList)
        {
            if (!hashSet.Add(obj.TestValue))
            {
                obj.IsDuplicate = true;
            }
        }

When you add a value first time to the HashSet, it adds successfully and HashSet.Add() method returns true so you don't make any changes to the item. When you're trying to add it second time, HashSet.Add() returns false and you mark your item as a duplicate.

The list will have the following state after finishing running our marking duplicates method:

Matt
Bob
Alice
Claire
Matt DUPLICATE

As others mentioned, the correct approach here would be to use the HashSet class.

        var hashSet = new HashSet<string>();

        foreach (var obj in testList)
        {
            if (!hashSet.Add(obj.TestValue))
            {
                obj.IsDuplicate = true;
            }
        }

When you add a value first time to the HashSet, it adds successfully and HashSet.Add() method returns true so you don't make any changes to the item. When you're trying to add it second time, HashSet.Add() returns false and you mark your item as a duplicate.

As others mentioned, the correct approach here would be to use the HashSet class.

        var hashSet = new HashSet<string>();

        foreach (var obj in testList)
        {
            if (!hashSet.Add(obj.TestValue))
            {
                obj.IsDuplicate = true;
            }
        }

When you add a value first time to the HashSet, it adds successfully and HashSet.Add() method returns true so you don't make any changes to the item. When you're trying to add it second time, HashSet.Add() returns false and you mark your item as a duplicate.

The list will have the following state after finishing running our marking duplicates method:

Matt
Bob
Alice
Claire
Matt DUPLICATE
Source Link
Ivan Yurchenko
  • 3.8k
  • 1
  • 25
  • 35

As others mentioned, the correct approach here would be to use the HashSet class.

        var hashSet = new HashSet<string>();

        foreach (var obj in testList)
        {
            if (!hashSet.Add(obj.TestValue))
            {
                obj.IsDuplicate = true;
            }
        }

When you add a value first time to the HashSet, it adds successfully and HashSet.Add() method returns true so you don't make any changes to the item. When you're trying to add it second time, HashSet.Add() returns false and you mark your item as a duplicate.