Find common items in list of lists of strings

kobosh

Hi I have allLists that contains lists of string I want to find common items among these string lists i have tried

var intersection = allLists
  .Skip(1)
  .Aggregate(
  new HashSet<string>(allLists.First()),
  (h, e) => { h.IntersectWith(e); return h);`

and also intersection ( hard code lists by index) all of them did not work when I tried

var inter = allLists[0].Intersect(allLists[1]).Intersect(allLists[2])
     .Intersect(allLists[3]).ToList();

foreach ( string s in inter) Debug.WriteLine(s+"\n ");

So how am I going to do this dynamically and get common string items in the lists; is there a way to avoid Linq?

t3chb0t

I'd do it like this:

class Program
{
    static void Main(string[] args)
    {
        List<string>[] stringLists = new List<string>[] 
        { 
            new List<string>(){ "a", "b", "c" },
            new List<string>(){ "d", "b", "c" },
            new List<string>(){ "a", "e", "c" }
        };

        // Will contian only 'c' because it's the only common item in all three groups.
        var commonItems = 
            stringLists
            .SelectMany(list => list)
            .GroupBy(item => item)
            .Select(group => new { Count = group.Count(), Item = group.Key })
            .Where(item => item.Count == stringLists.Length);

        foreach (var item in commonItems)
        {
            Console.WriteLine(String.Format("Item: {0}, Count: {1}", item.Item, item.Count));
        }
        Console.ReadKey();
    }
}

An item is a common item if it occurs in all groups hence the condition that its count must be equal to the number of groups:

.Where(item => item.Count == stringLists.Length)

EDIT:

I should have used the HashSet like in the question. For lists you can replace the SelectMany line with this one:

.SelectMany(list => list.Distinct())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find common items in list of lists of strings

From Dev

Find items not in common between lists in Python

From Dev

Function to find longest common subsequences of a list of lists?

From Dev

Find similar items in list of lists using Python

From Dev

find lists that start with items from another list

From Dev

Find duplicate items in all lists of a list of lists and remove them

From Dev

Find common sub string from the list of strings

From Java

Find all pairs of strings in two lists that contain no common characters

From Dev

Using jQuery to find list items containing specific text in nested lists

From Dev

How to find common part among all items in the string list?

From Dev

convert list of lists with time strings to seconds and find count times between

From Dev

find if there is a common string between 2 list of strings using linq

From Dev

Python: return list of sequentially occuring common items from lists and also a list of uncommon ones

From Dev

Editing strings in a list of lists

From Dev

Editing strings in a list of lists

From Dev

SQL query to find common items within the lists of columns between a table and specified criteria?

From Dev

Are these list items strings or not?

From Dev

Merging a list of strings and a list of lists

From Dev

Convert a list of strings and lists into a list of chars and lists?

From Dev

Convert a list of strings and lists into a list of chars and lists?

From Dev

How to find a document with an array of strings based on if it has items in common with a reference array of string?

From Dev

How to find a document with an array of strings based on if it has items in common with a reference array of string?

From Dev

Split list of strings into list of lists of strings

From Dev

How to find matching strings for lists inside lists?

From Dev

Fastest way to compare common items in two lists

From Dev

Python: Returning items common to two or more lists

From Dev

Searching Items at List has Lists in it

From Dev

Insert items to lists within a list

From Dev

Find length of a list of lists

Related Related

  1. 1

    Find common items in list of lists of strings

  2. 2

    Find items not in common between lists in Python

  3. 3

    Function to find longest common subsequences of a list of lists?

  4. 4

    Find similar items in list of lists using Python

  5. 5

    find lists that start with items from another list

  6. 6

    Find duplicate items in all lists of a list of lists and remove them

  7. 7

    Find common sub string from the list of strings

  8. 8

    Find all pairs of strings in two lists that contain no common characters

  9. 9

    Using jQuery to find list items containing specific text in nested lists

  10. 10

    How to find common part among all items in the string list?

  11. 11

    convert list of lists with time strings to seconds and find count times between

  12. 12

    find if there is a common string between 2 list of strings using linq

  13. 13

    Python: return list of sequentially occuring common items from lists and also a list of uncommon ones

  14. 14

    Editing strings in a list of lists

  15. 15

    Editing strings in a list of lists

  16. 16

    SQL query to find common items within the lists of columns between a table and specified criteria?

  17. 17

    Are these list items strings or not?

  18. 18

    Merging a list of strings and a list of lists

  19. 19

    Convert a list of strings and lists into a list of chars and lists?

  20. 20

    Convert a list of strings and lists into a list of chars and lists?

  21. 21

    How to find a document with an array of strings based on if it has items in common with a reference array of string?

  22. 22

    How to find a document with an array of strings based on if it has items in common with a reference array of string?

  23. 23

    Split list of strings into list of lists of strings

  24. 24

    How to find matching strings for lists inside lists?

  25. 25

    Fastest way to compare common items in two lists

  26. 26

    Python: Returning items common to two or more lists

  27. 27

    Searching Items at List has Lists in it

  28. 28

    Insert items to lists within a list

  29. 29

    Find length of a list of lists

HotTag

Archive