Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So first of all, I think that to be clear what you're asking for is the largest phrase, for lack of a better word. The largest substrings I see in the example array are actually <code>"carflam f"</code> and <code>" peanut butter"</code>. And feel free to change the <code>ary</code> argument if that's a known quantity in whatever class you're using:</p> <pre><code>def get_array_of_phrases_larger_than(ary, min) all = [] # Ugly, but this will span the range of possible phrases for each item in the # array, building them into a one-dimensional array if they meet the minimum # length requirements ary.each do |phrase| words = phrase.split last = words.length - 1 (0..last).each do |from| (from..last).each do |to| p = words[from..to].join(" ") all &lt;&lt; p if p.size &gt; min end end end # Get a list of all repeated keys repeated = all.group_by(&amp;:to_s).select { |_, v| v.size &gt; 1 } keys = repeated.keys # Get a list of the longest keys, such that we exclude "peanut" and "butter" # if "peanut butter" exists longest = repeated.select do |key, _| keys.select { |k| k.include?(key) }.size == 1 end # Sort in reverse order by length longest.keys.sort_by { |k| -k.size } end @ary = ["carflam fizz peanut butter", "fizz foo", "carflam foo peanut butter"] get_array_of_phrases_larger_than @ary, 3 # =&gt; ["peanut butter", "carflam", "fizz"] </code></pre> <p>Note that this would be agnostic to where the strings come from, so you could potentially have a false positive with something like <code>["butter butter", "foo", "baz"]</code> returning <code>["butter"]</code>, but I'll leave that as an exercise to the reader.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload