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

  • Home
  • SEARCH
  • 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 8993319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:10:08+00:00 2026-06-15T23:10:08+00:00

Given an array that gets populated with strings. I need following behavior: foo =

  • 0

Given an array that gets populated with strings. I need following behavior:

foo = []
foo = add_search_string(foo, 'a')

foo should equal [‘a’]

foo = add_search_string(foo, 'a')

foo should equal [‘a’] because ‘a’ was already a search string

foo = add_search_string(foo, 'ab')

foo should equal [‘ab’] because ‘a’ is a substring of ‘ab’ and therefore can be removed

foo = add_search_string(foo, 'a')

foo should equal [‘ab’] because of the same reason as above

foo = add_search_string(foo, 'c')

foo should equal [‘ab’, ‘c’]

My function looks like this:

function add_search_string(search_strings, new_search_string) {
    var keep = true;
    var new_search_strings = []
    $.each(search_strings, function(i, search_string) {
        if (new_search_string == search_string) {
            keep = false;
        } else if (search_string.indexOf(new_search_string) >= 0) {
            keep = false;
        }
    });

    if (keep) {
        $.each(search_strings, function(i, search_string) {
            if (new_search_string.indexOf(search_string) == -1) {
                new_search_strings.push(search_string);
            }
        });
        new_search_strings.push(new_search_string);
        search_strings = new_search_strings;
    }
    return search_strings;
}

Is there a ‘better’ way to do this?

  • 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-06-15T23:10:09+00:00Added an answer on June 15, 2026 at 11:10 pm

    If the intention is to keep updating the same array I’d probably do something like this:

    function add_search_string(search_strings, new_search_string) {
       var replaced = false;
       for (var i = search_strings.length -1; i >= 0; i--) {
          if (search_strings[i].indexOf(new_search_string) != -1) {
              // string found, so just return
              return search_strings;
          }
          if (new_search_string.indexOf(search_strings[i]) != -1){
              // existing string is a substring of new search string
              // if it already matched another element just remove the current one
              // otherwise replace the current one
              if (replaced)
                  search_strings.splice(i,1);
              else
                  search_strings[i] = new_search_string;
              replaced = true;
          }
       }
       // if not found add it
       if (!replaced)
          search_strings.push(new_search_string);
       return search_strings;
    }
    

    Although this function returns the array, it also updates the array you pass in so you don’t have to assign it back when you call the function, you can just say:

    add_search_string(foo, 'a');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hidden field that gets populated with a javascript array of ID's.
I have a homework question that says: Problem 1: Given the array [ 22
I was reading a book when I found that array size must be given
QUESTION: Given a cell index (red) compute the array index (black) that surround the
Given that EVAL is Evil how do I create an Array name dynamically: I
Design an algorithm that, given a list of n elements in an array, finds
It is written byte[][] getImagesForFields(java.lang.String[] fieldnames) Gets an array of images for the given
I have class AClass and a method someMethod that gets an Object array as
I have a 2D array that gets dynamically created by any integer N. Initially,
Good day, I have been given an assignment that gets data off a CSV

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.