-2

i have one list object which contains int like below:

var list = new List<int>();
var ids=getting only ids from database.for Eg:1,2,1,3,5,6,3.

Now what i want to do is after fetching ids from datatabase i want to add in to my list object and if any differnt ids is added(eg:ids:2) then i want to break from the loop.

For Ex: After adding 1 in my list now if i try to add 2 which doesnt exist in my list object then i want to break from my loop with status different element found outside the loop.

Add values in the list object till duplicate value is found means add only duplicate values in my list object and if distinct value is found which is not already in the list then break from loop.Break from the loop after adding 1 in list because 2 is different and it is not already in the list object.

This is my code:

bool status;
for (var i = 0; i < ids.Count; i++)
{
    list.Add(ids);
}
//here i want status with different element found or not

Note:I just want to add only duplicates values in my list object until new id is found.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

4 Answers4

2
  • You can use foreach to iterate over elements of ids
  • break works in foreach just the way it does in for
  • if we don't break, do whatever you want to do, for example, add the value to list
  • Contains checks if list already contains id.
    • This makes it roughly an O(n^2) operation - because both foreach and Contains iterate over elements of list

Code:

foreach (var id in ids)
{
  if (list.Contains(id))
  {
    break;
  }
  list.Add(id);
}
Amadeusz Wieczorek
  • 3,139
  • 2
  • 28
  • 32
  • Please add some explanation to your answer! – ρss Jan 22 '16 at 08:12
  • i was expecting asnwer like what Tim Tim Schmelter has given on this question:http://stackoverflow.com/questions/18303897/test-if-all-values-in-a-list-are-unique – I Love Stackoverflow Jan 22 '16 at 09:11
  • 1
    @Learning if you expect this kind of answer, better communicate what you already did and what you want to do - judging by your question, I thought your problem is trivial and that you don't need extremely optimized code. – Amadeusz Wieczorek Jan 22 '16 at 17:55
2

i just want to add duplicate values in list object until different ids is found.

var list = new List<int>{ 1, 2, 3, 4};
var idsFromDb = new List<int> {1, 2, 5, 3};

foreach (int id in idsFromDb)
{
    if (list.Contains(id))
    {
        break;
    }
    list.Add(id);
}
Lukasz Pyrzyk
  • 418
  • 4
  • 9
  • i was expecting asnwer like what Tim Schmelter has given on this question:http://stackoverflow.com/questions/18303897/test-if-all-values-in-a-list-are-unique – I Love Stackoverflow Jan 22 '16 at 09:11
1

You can use Contains

status = false;
for (var i = 0; i < ids.Count; i++)
{
    if (list.Count > 0 && !list.Contains(ids[i])){            
        list.Add(ids[i]); //add this, be it duplicate or not
        status = true; //different element found
        break; //break if this is not duplicate
    } else {
        //do something for duplicate
        list.Add(ids[i]); //add this, be it duplicate or not
    }
}

Contains can check if your item already exists in the List

Ian
  • 30,182
  • 19
  • 69
  • 107
1

Here's your desired HashSet<T> + LINQ approach:

var dupChecker = new HashSet<int> {ids[0]};  // add first element, now only duplicates are allowed
List<int> dupList = ids.TakeWhile(i => !dupChecker.Add(i)).ToList();
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939