-1

I've got two List<string>, one contains user's IDs and their emails, second contains only emails. I am trying to find duplicated emails from second list in the first list, and input email's IDs in a variable.

Example: 1st List content:

emailswithIDs = List<string> {1, abc@gmail.com, 2, abcd@gmail.com, 3, abcde@gmail.com

2nd List content:

emails = List<string> {abcd@gmail.com}

So I'd like to make variable "IDs" which would contain "2" as it is the ID of duplicated email.

I have tried:

var IDs = emails.Intersect(emailswithIDs);

But obviously it will only get the duplicated string, and I need to get the email's ID value.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
Pastooh
  • 1
  • 2
  • Did you review [this post](https://stackoverflow.com/questions/21148175/compare-two-liststring-and-print-the-duplicates)? – Sercan Dec 12 '21 at 16:30

3 Answers3

0

You can try this one:

List<string> emailsWithIDs = new List<string> { "1", "abc@gmail.com", "2", "abcd@gmail.com", "3", "abcde@gmail.com", "4", "abcde@gmail.com"};
List<string> emails = new List<string> { "abcd@gmail.com" };
List<string> IDs = new List<string>();

for(int i=0; i<emailsWithIDs.Count(); i+=2)
{
    for(int j=0; j<emails.Count(); j++)
    {
        if(emailsWithIDs[i+1] == emails[j])
        {
            IDs.Add(emailsWithIDs[i]);
        }
    }
}

Console.WriteLine(IDs.Count());

Console.ReadKey();
0

Mixing ids and values in the same List<string> is not a good practice. Let's build a dictionary (i.e. mail - id correspondence): mail will be a Key and id will be a Value:

  Dictionary<string, string> emailToId = new Dictionary<string, string>(
    StringComparer.OrdinalIgnoreCase);

  for (int i = 0; i < emailswithIDs.Count; i += 2) 
    emailToId.Add(emailswithIDs[i + 1], emailswithIDs[i]);

Then you can easily query if item in emails exists in emailToId:

  var ids = emails
    .Where(email => emailToId.ContainsKey(email))
    .Select(email => emailToId[email]);  

Please, fiddle

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • I know that it is not a good practice, but unfortunately both of the lists that I've got are created by two different APIs, I am trying to compile the data for further use in one of the APIs. – Pastooh Dec 12 '21 at 16:45
  • 1
    @Pastooh: well, you can get data from the API, but then turn list into a dictionary and from that moment on work with the dictionary. – Dmitry Bychenko Dec 12 '21 at 16:47
  • 1
    Thank you so much, I've changed my code a bit and your answer resolved my problem. – Pastooh Dec 12 '21 at 17:59
0

If finding an email address at index n and you can guarantee that element at n-1 is the corresponding id, you can do something like this:

// For each entry in emails find the index of the same string
// In the emailswithIDs list, then take n-1 because that
// represents the index of the id in emailswithIDs
var duplicateEmailIndices = emails
    .Select(x => emailswithIDs.IndexOf(x))
    .Where(x => x >= 1)
    .Select(x => x - 1);

// Get the entries from emailswithIDs at each index given
// in duplicateEmailIndices
var duplicateIds = emailswithIDs.Where((id, index) => duplicateEmailIndices.Contains(index));

This, of course, only provides a solution for the code as it is written currently. There are better solutions, such as Dictionary, as pointed out in another answer. It also assumes that the emailswithIDs list will always contain data in the order of id, email, id, email...

Matt U
  • 4,970
  • 9
  • 28