Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 387923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:43:11+00:00 2026-05-12T15:43:11+00:00

I have a list of words in which some are composed words, in example

  • 0

I have a list of words in which some are composed words, in example

  • palanca
  • plato
  • platopalanca

I need to remove “plato” and “palanca” and let only “platopalanca”.
Used array_unique to remove duplicates, but those composed words are tricky…

Should I sort the list by word length and compare one by one?
A regular expression is the answer?

update: The list of words is much bigger and mixed, not only related words

update 2: I can safely implode the array into a string.

update 3: I’m trying to avoid doing this as if this was a bobble sort. there must be a more effective way of doing this

Well, I think that a buble-sort like approach is the only possible one 🙁
I don’t like it, but it’s what i have…
Any better approach?

function sortByLengthDesc($a,$b){
return strlen($a)-strlen($b);
}

usort($words,'sortByLengthDesc');
$count = count($words);
for($i=0;$i<=$count;$i++) {
    for($j=$i+1;$j<$count;$j++) {
        if(strstr($words[$j], $words[$i]) ){
            $delete[]=$i;
        }
    }
}
foreach($delete as $i) {
    unset($words[$i]);
}

update 5: Sorry all. I’m A moron. Jonathan Swift make me realize I was asking the wrong question.
Given x words which START the same, I need to remove the shortests ones.

  • “hot, dog, stand, hotdogstand” should become “dog, stand, hotdogstand”
  • “car, pet, carpet” should become “pet, carpet”
  • “palanca, plato, platopalanca” should become “palanca, platopalanca”
  • “platoother, other” should be untouchedm they both start different
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:43:11+00:00Added an answer on May 12, 2026 at 3:43 pm

    I think you need to define the problem a little more, so that we can give a solid answer. Here are some pathological lists. Which items should get removed?:

    • hot, dog, hotdogstand.
    • hot, dog, stand, hotdogstand
    • hot, dogs, stand, hotdogstand

    SOME CODE

    This code should be more efficient than the one you have:

    $words = array('hatstand','hat','stand','hot','dog','cat','hotdogstand','catbasket');
    
    $count = count($words);
    
    for ($i=0; $i<=$count; $i++) {
        if (isset($words[$i])) {
            $len_i = strlen($words[$i]);
            for ($j=$i+1; $j<$count; $j++) {
                if (isset($words[$j])) {
                    $len_j = strlen($words[$j]);
    
                    if ($len_i<=$len_j) {
                        if (substr($words[$j],0,$len_i)==$words[$i]) {
                            unset($words[$i]);  
                        }
                    } else {
                        if (substr($words[$i],0,$len_j)==$words[$j]) {
                            unset($words[$j]);
                        }
                    }
                }
            }
        }
    }
    
    foreach ($words as $word) {
        echo "$word<br>";
    }
    

    You could optimise this by storing word lengths in an array before the loops.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List<string> which has some words duplicated. I need to find all
I have a text file which contains a long list of words. Some of
I have two files which both contain a list of words. Is there an
I have a string which is basically a list of words delimited by commas.
Let's say I have a list of words, something like this: ['The', 'Quick', 'Brown',
I have a list of ~20,000 email addresses, some of which I know to
I have two lists. I need to determine which word from the first list
Greetings All; I have a LinkedList of type String which contain some words like
I have a membase server and Have a list of words which is my
I have plans to create an app which gives me the list of words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.