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 1000785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:34:04+00:00 2026-05-16T07:34:04+00:00

I have a string like this: // string1 horse|cow|goat|zebra| and another string like this:

  • 0

I have a string like this:

// string1
horse|cow|goat|zebra|

and another string like this:

// string2
horse:a,pig:b,cow:z,monkey:g,goat:a,

my goal is to split string1, then pick out any occurrences of it in string2, to build a histogram. I am currently doing this:

var histogram = {};

var animals = string1.split("|");
for (var i = 0; i < animals.length; i++) {
    var animal = animals[i];
    var animalColon = animal + ":";

    var index = string2.indexOf(animalColon);
    while (index != -1) {
        var indexColon = index + animalColon.length;
        var indexFinal = string2.indexOf(",", indexColon);
        var letter = string2.substring(indexColon, indexFinal);

        if (histogram[letter] == null) {
            histogram[letter] = 1;
        }
        else {
            histogram[letter] = histogram[letter] + 1;
        }
        index = string2.indexOf(animalColon, index + 1);
    }
}

at the end, it might print something like:

// histogram:
a: 2 instances // from { horse, goat }
z: 1 instance  // from { cow }

the above will work, but I have to dp animals.length passes through string2 to check everyone.

Is there a way to use regular expressions to do this parsing – essentially run all tests in parallel, instead of doing multiple passes through? Since string2 is const, it seems that all checks could be done simultaneously (not sure if regexes are implemented like this).

I increased the number of elements in string1 and string2 on the order of thousands of elements and it still runs quite fast, but am worried about slower machines, maintainability, and stuff like that,

Thanks

  • 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-16T07:34:04+00:00Added an answer on May 16, 2026 at 7:34 am

    I’d start by pre-processing your string2, which you say is constant. Working with an object is better than keep searching in the string:

    var s = "horse:a,pig:b,cow:z,monkey:g,goat:a";
    var hash = {};
    var tokens = s.split(',');
    for(var i=0;i<tokens.length;i++){
        var a = tokens[i].split(':');
        hash[a[0]] = a[1];
    }
    

    Next, when you get the string, you have easier time looking up the letters (you may also want to check for if(letter), if you get a new animal in string1):

    var histogram = {};
    var string1 = "horse|cow|goat|zebra";
    var animals = string1.split("|");
    for(var i=0;i<animals.length;i++){
        var letter = hash[animals[i]];
        if (!histogram[letter])
            histogram[letter] = 0;
        histogram[letter]++;
    }
    

    As per your question, you could probably abuse regular expression to count the letters, but it isn’t parallel, but linear at best, and probably complex enough not to worthwhile.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer ActiveRecord is fairly memory-hungry - be very careful when doing… May 16, 2026 at 12:42 pm
  • Editorial Team
    Editorial Team added an answer HANDLE is a pointer to void, and Microsoft's compiler allows… May 16, 2026 at 12:42 pm
  • Editorial Team
    Editorial Team added an answer Probably QWidget::setWindowState will suit you and the state for minimized… May 16, 2026 at 12:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a string like this <name>sekar</name> . I want to split this string
Let's say I have a class like this: public class Person { private String
Say I have a string like this: $string = 'The /*quick*/ brown /*fox*/ jumped
I made a simple javascript class like this: function Horse() { this.color = 'brown';
MY table looks like this id | string | foreign_id --------------------------- 1 | house
I have this string here: CREATE UNIQUE INDEX index555 ON SOME_TABLE ( SOME_PK ASC
let's say im returning a datetime string like this in JS: 8/18/2010 9:35:27 AM
Usually when my regex patterns look like this: http://www.microsoft.com/ Then i have to escape
I have one array that contains some settings that looks like basically like this:
I love challenges like this, I'll hopefully submit my answer soon. Which player has

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.