0

How can I extract all words, which are common in a list of strings?

Example:

//Output = Bammler GOV
  "Bammler Tokyo SA GOV"
  "Zurich Bammler GOV"
  "London Bammler 12 GOV"
  "New Bammler York GOV"

I tried following:

    static void Main(string[] args)
    {
        List<string> MyStringList = new List<string>()
        {
            "Bammler Tokyo SA GOV",
            "Zurich Bammler GOV",
            "London Bammler 12 GOV",
            "New Bammler York GOV"
        };

        string shortest = MyStringList.OrderBy(s => s.Length).First();
        IEnumerable<string> shortestSubstrings = getAllSubstrings(shortest).OrderByDescending(s => s.Length);
        var other = MyStringList.Where(s => s != shortest).ToArray();
        string longestCommonIntersection = string.Empty;
        foreach (string subStr in shortestSubstrings)
        {
            bool allContains = other.All(s => s.Contains(subStr));
            if (allContains)
            {
                longestCommonIntersection = subStr;
                break;
            }
        }
    }

    public static IEnumerable<string> getAllSubstrings(string word)
    {
        return from charIndex1 in Enumerable.Range(0, word.Length)
               from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
               where charIndex2 >= 2
               select word.Substring(charIndex1, charIndex2);
    }

I found this here Find a common string within a list of strings but this will just extract for example "Bammler".

2
  • 2
    What have you tried? You can't expect us to do all the work for you... Commented Jul 18, 2017 at 15:00
  • 1
    StackOverflow works this way - you try to solve task on your own, and if you have some problems (error or unexpected results), then you give problem description, your current code and ask people to help you. Commented Jul 18, 2017 at 15:00

2 Answers 2

4

You can aggregate result of words intersection from all strings:

var result = MyStringList.Select(s => s.Split())
    .Aggregate(
         MyStringList[0].Split().AsEnumerable(), // init accum with words from first string
         (a, words) => a.Intersect(words),       // intersect with next set of words
         a => a);

Output:

[
  "Bammler",
  "GOV"
]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Sergey!
2

I would go with @Sergey answer, but I want to add you can also use a hash to get the intersection:

var list = new  List < string >{  "Bammler Tokyo SA GOV",
                                  "Zurich Bammler GOV",
                                  "London Bammler 12 GOV",
                                  "New Bammler York GOV"};

var hash = new HashSet<string> ( list.First().Split(' ') );
for (int i = 1; i < list.Count; i++)
    hash.IntersectWith(list[i].Split(' '));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.