Quickest way to find the string in 1 dimensional string array!
I have a queue 1 dimensional array of strings called 'queue' and I need
a fast way to search it. Once there is match, I don't need to search
any longer.
Currently I am using this code.
But I think it's too slow, because it runs through whole dimension.
I know this is trivial question, but is there any way to stop this
loop, or better way to search? I mean - FASTER?
Dim queue(1000000) As String
Dim lookfor as String
Dim count, isthere as Integer
While queue(count) <> Nothing
If queue(count) = lookfor Then
isthere = 1
End If
listing2 = count + 1
End While
If isthere = 1 then messagebox.show ("Found it!")
else messagebox.show ("Didn't Find it!")
End if
Please let me know.
Joe 3 4032
"Jozef Jarosciak" <jo*@doprocess. com> schrieb: Quickest way to find the string in 1 dimensional string array!
\\\
Dim Items() As String = ...
Dim Pattern As String = ...
Dim Found As Boolean
For Each Item As String In Items
If Item = Pattern Then
Found = True
Exit For
End If
Next Item
MsgBox(Found)
///
The code above is a linear search, which will check items until the item is
found. Its runtime is in O(n) for n items. A faster search algorithm is
Binary Search, which requires sorted data. If your data is already sorted
or you plan to perform multiple queries for the same database, check out the
array's 'Sort' and 'BinarySearch' method. The runtime of 'Sort' is Theta(n
log(n)) for n items, and 'BinarySearch' can be performed in O(log n) for n
items.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jozef,
In addition to Herfried's response, if the name of your array suggests its
use, there is a Queue class that you might find useful.
Kerry Moorman
"Jozef Jarosciak" wrote: Quickest way to find the string in 1 dimensional string array!
I have a queue 1 dimensional array of strings called 'queue' and I need a fast way to search it. Once there is match, I don't need to search any longer. Currently I am using this code. But I think it's too slow, because it runs through whole dimension. I know this is trivial question, but is there any way to stop this loop, or better way to search? I mean - FASTER?
Dim queue(1000000) As String Dim lookfor as String Dim count, isthere as Integer
While queue(count) <> Nothing If queue(count) = lookfor Then isthere = 1 End If listing2 = count + 1 End While
If isthere = 1 then messagebox.show ("Found it!") else messagebox.show ("Didn't Find it!") End if
Please let me know. Joe
Herfried,
thanks a lot for the code. I am going to use it. Thanks once again for
your time.
This was very helpful.
Joe This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Deano |
last post by:
I think my brain has short-circuited again :)
Is this the quickest way to check for the existence of a given value in an
array?
e.g
For i = 0 To rrst.RecordCount
If myArray(i) = DLookup("ID", "tblmain", "Salary = " &
varSomeValue) Then
|
by: Alex Munk |
last post by:
I would like to be able to pass a 2-dimensional array to a function as a string parameter. In the called function I would like to be able to convert the string back into a 2-dimensional array. Example
Function1 {
int TheArray;
Function2(TheArray.ToString();
}
Function2 (string parm) {
.... In here I would like to be able to convert parm back into a 2-dimensional int array. How do I do this
}
|
by: Jozef Jarosciak |
last post by:
Quickest way to find the string in 1 dimensional string array!
I have a queue 1 dimensional array of strings called 'queue' and I need
a fast way to search it. Once there is match, I don't need to search
any longer.
Currently I am using this code.
But I think it's too slow, because it runs through whole dimension.
I know this is trivial question, but is there any way to stop this
loop, or better way to search? I mean - FASTER?
|
by: Sean |
last post by:
Hello,
I have a function in C# that accepts a stored proc name and an array of parameters, and then returns a dataset, which works just fine.
I have converted the function to VB:
Public Shared Function GetDataSet(ByVal storedProcedure As String, ByRef parameterArray As Object()) As DataSet
|
by: baret bonden |
last post by:
I get :Value of type 'String' cannot be converted to '1-dimensional array
of String' refering to curitem
Dim curItem As String
curItem = ListBox1.SelectedItem
TextBox1.Text = curItem
vcar = curItem
| |
by: Jackson |
last post by:
I have something that is stumping me. I am trying to initialize a 3
dimensional string array with the code below, but it wont compile. Can anyone
explain what Im doing wrong?????????????? Im going crazy with this one! Ive
tried a 2 dimensional array minus the "test3" and it works fine.
public string screens;
screens = new string{ {"test1","test2","test3"} };
|
by: trpost |
last post by:
I am looking for a way to find the differences between 2
multidimensional arrays. I have found ways to do this based on 1 key,
but I want to be able to look at differences based on all keys:
For example:
Array 1
=>
array(6) {
|
by: per9000 |
last post by:
Hi all,
I have a two-dimensional array of data, f.x int's. We can imagine that
the array is "really large". Now I want the data in it and store this
in a one-dimensional array.
The obvious way to do this is a nested for-loop - but we all know
O(n^2) is bad. So I am looking for something like ArrayList.ToArray(),
or Matlabs A(:).
C#
|
by: Paulers |
last post by:
Hello,
I have a string that I am trying to add each char to a datatable row.
for example if I have a string that looks like "abcdefg", I would like
to break it up into an array of characters so I can do this:
myDataTable.Rows.Add(array())
instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
|
by: nelly0 |
last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six different models of cars that are produced by the car manufacturer.
Task 1
Step 1: Create a directory called Assignment02 and create the files of steps 2, 3 and 4 in this directory as well as your project file.
Step 2: Create a file called...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |