Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How can C# match substrings very fast?

Avatar of chaffinsjc
chaffinsjc asked on
3 Comments1 Solution535 ViewsLast Modified:
In the problem example below, each item in "usableItems" is looked for in the "originalItems" data structure. Each item that is not found is placed in the discardedItems string array.

string[] usableItems = {"1", "b7", "13" };
string[] originalItems = {"1", "3", "5", "b7", "9", "11", "13" };
string[] discardedItems = { "3", "5", "9", "11" }; //final result

How should a C# program structure the data and logic to minimize the time to build the final string array named "discardedItems"?

I think Regex and IndexOf would probably be slow. And I'm not sure about using char[] arrays somehow. Also, there might be consideration of a dictionary for quick lookup, but then establishing the dictionary will also require time. Hmmm.

SOME RULES (just want to explain clearly):
(1) The items numerically represent the notes in common piano chords. There are hundreds of chords, with variations in sharps, flats, and number of items to match and/or be matched. Typical items are "3", "#5", "7", "b7", "#9", "b11", "11", "#11", "b13", etc.
(2) The items always increase numerically from left to right.
(3) Items do not repeat.
(4) There can be from 1 to 7 "originalItems".
(5) There can be from 1 to 7 "usableItems".
(6) There will never be more "usableItems" than "originalItems".
(7) Each item in "usableItems" ALWAYS DOES EXIST in "originalItems".

NOTE: To present this problem, string arrays are used for "usableItems" and "originalItems", but only the final result "discardedItems" must be a string array.

I would sincerely appreciate some code ideas.
Avatar of Fernando Soto
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers