Home > Software engineering >  C# How to give same index to common items in 2 lists
C# How to give same index to common items in 2 lists

Time:05-28

I have Two lists of int type list.

firstlist = new List<int>() { 1, 4, 9, 14, 19, 24  };
secondlist = new List<int>() { 2, 3, 4, 19, 22, 23  };

i want to find common items of firstlist in secondlist and give same index to same item in secondlist . Then I want secondlist be sorted like this: secondlist = { 2, 4, 3, 22, 19, 23 };

Actually I want that common items in both lists have same index.

I can do it with some loop and if statements but it takes very long code when lists have more and more items. for example when each list has 3 members:

firstlist = new List<int>() { 1, 4, 9 };
secondlist = new List<int>() { 2, 3, 4 };

for (int i = 0; i < firstlist.Count; i  )
        {
            
            {
                
                if (firstlist.Contains(secondlist[i]) ))
                {
                    if(secondlist[i]==4)
                    {
                        qs1 = secondlist[0];
                        secondlist[0]= secondlist[i];
                        secondlist[i] = qs1;
                    }
                    else if (secondlist[i] == 9)
                    {
                        qs1 = secondlist[1];
                        secondlist[1] = secondlist[i];
                        secondlist[i] = qs1;
                    }
                    else if (secondlist[i] == 14)
                    {
                        qs1 = secondlist[2];
                        secondlist[2] = secondlist[i];
                        secondlist[i] = qs1;
                    }
    }

CodePudding user response:

You can use the Lists Intersect function to get the common items. Then swap the numbers in the second list accordingly.

var firstlist = new List<int>() { 1, 4, 9, 14, 19, 24 };
var secondlist = new List<int>() { 2, 3, 4, 19, 22, 23 };

var common = firstlist.Intersect(secondlist);

foreach (var n in common)
{
    var fIndex = firstlist.FindIndex(f => f == n);
    var sIndex = secondlist.FindIndex(s => s == n);
    var temp = secondlist[fIndex];
    secondlist[fIndex] = n;
    secondlist[sIndex] = temp;
}

CodePudding user response:

Do it straightforwardly with for loop

for (int i = 0; i < firstlist.Count; i  )
{
    var index = secondlist.IndexOf(firstlist[i]);
    if (index >= 0)
    {
        secondlist.RemoveAt(index);
        secondlist.Insert(i, firstlist[i]);
    }
}

CodePudding user response:

The solution I came up with accounts for a few cases that your question has not specified:

  • The lists may be different lengths
  • The lists may have repeating common items

Either of these situations can result in incorrect behavior from the previous solutions.

I've included a Main method with some test data to cover these situations.

void Main()
{
    var list1 = new List<int>() { 1, 4, 9, 14, 19, 24 };
    var list2 = new List<int>() { 2, 3, 4, 19, 22, 23 };

    var list3 = new List<int>() { 1, 4, 9, 14, 19, 4 };
    var list4 = new List<int>() { 2, 3, 4, 19, 4, 23 };

    var list5 = new List<int>() { 1, 4, 9, 14, 19 };
    var list6 = new List<int>() { 2, 3, 4, 19, 4, 23 };

    var list7 = new List<int>() { 1, 4, 9, 4, 6, 19, 4 };
    var list8 = new List<int>() { 2, 3, 4, 19, 8, 4};

    SortCommon(list1, list2);
    SortCommon(list3, list4);
    SortCommon(list5, list6);
    SortCommon(list7, list8);
}

void SortCommon(List<int> first, List<int> second)
{
    for (var i = 0; i < second.Count && i < first.Count; i  )
    {
        //We check for common items in a loop, as multiple common items of the same value
        //should be sorted in order
        for (var index = 0; index   >= 0 ; )
        {
            index = second.IndexOf(first[i], index);
            //Need to make sure that we check to see if the item at index has already been sorted
            if (index >= 0 && second[index] != first[index])
            {
                second[index] = second[i];
                second[i] = first[i];
                break;
            }
        }
    }
    Console.WriteLine($"First:  {string.Join(", ", first)}");
    Console.WriteLine($"Second: {string.Join(", ", second)}");
}
  •  Tags:  
  • c#
  • Related