473,569 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quickest way to find the string in 1 dimensional string array!

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?

This code is in vb.net. But I need a way to do this in C# as well.

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

Nov 17 '05 #1
6 8949
Rather than a 1 dimensional array you should probably be storing your
strings in a hashtable using the generated hash of the string as the key.

The hashtable will give a much faster result for match or no-match on any
given string.

Knowing what this was really for would also enable people to suggest a more
efficient method.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Jozef Jarosciak" <jo*@doprocess. com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
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?

This code is in vb.net. But I need a way to do this in C# as well.

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

Nov 17 '05 #2
Jozef Jarosciak <jo*@doprocess. com> 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?


Use Array.IndexOf in this case. In general though, to exit a loop
early, use break.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
On 9 Jul 2005 13:57:20 -0700, "Jozef Jarosciak" <jo*@doprocess. com>
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?

This code is in vb.net. But I need a way to do this in C# as well.

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


An alternative:

Use a sorted array instead of your queue. Use binary search to look
for the target string.

As Bob said, we need more information to help determine what solutin
might work best for you.

rossum

The ultimate truth is that there is no ultimate truth
Nov 17 '05 #4
I am building a web crawler. Soon I will release the code under GNU.
Basically, as I am crawling the pages I need to check if new url is
already in the list.
So on small sites, this would work fine. But on the big sites where
there might be well over 1.000.000 urls, it could be a slow process.
I hope this helps to determine the best solution.
So far, thanks for all your help. I am looking forward for new ideas.

Nov 17 '05 #5
Hi,

Bob's advise of a hashtable would be a good choice for this type of task.

Hashtable gives you fast add and access operations.

You can use the StringDictionar y class in System.Collecti ons.Specialized , which
handles the strings in a case insensitive matter. I think this is what you want
for URLs, but I'm not 100% sure?? If you need case sensitive comparisons, just
use the regular Hashtable class.

StringDictionar y visitedURLs = new StringDictionar y();

....

// assuming url contains the URL to check...

if (!visitedURLs.C ontainsKey(url) )
{
// add url to visited hashtable
visitedURLs.Add (url);

// take appropriate action for URL's that have not been visited
}
// otherwise, URL has already been visited

Hope this helps.

Best regards,

Rodger

Achieve Planner - Keep track of your projects/tasks and schedule them in your
calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDia gramEditor.com>

Jozef Jarosciak wrote:
I am building a web crawler. Soon I will release the code under GNU.
Basically, as I am crawling the pages I need to check if new url is
already in the list.
So on small sites, this would work fine. But on the big sites where
there might be well over 1.000.000 urls, it could be a slow process.
I hope this helps to determine the best solution.
So far, thanks for all your help. I am looking forward for new ideas.

Nov 17 '05 #6

"Jozef Jarosciak" <jo*@doprocess. com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
I am building a web crawler. Soon I will release the code under GNU.
Basically, as I am crawling the pages I need to check if new url is
already in the list.
So on small sites, this would work fine. But on the big sites where
there might be well over 1.000.000 urls, it could be a slow process.
I hope this helps to determine the best solution.
So far, thanks for all your help. I am looking forward for new ideas.


Since you are dealing with URLs, which usually describe hiearchys, you might
be able to split the URL up into components and use a tree structure. It'd
likely be a little slower on small sites but chances are it would scale much
better than an array would. It'd also save space as you should be able to
use string interning to your advantage.

for a URL like http://host/folder/file you would look for "host" in the
first level of nodes, "folder" in the second, and "file" in the third.

I'm not sure it'd work, but I think it will and it's certainly worth a
little research.
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1544
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
3
2069
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...
2
5870
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
6
38062
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
3
4026
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...
5
7800
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{...
1
3301
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) {
8
11798
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#
5
3870
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...
0
8132
jinu1996
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...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
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...
1
5514
isladogs
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...
0
5222
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.