I have Structure like this,
String[] variable1= new String["ABC", "FSS" , "FSFS", "GDGDDS"];
String[] variable2= new String["SA", "GS" , "QE", "HF"];
static List<String[]> allList = new List<String[]>();;
allList .Add(variable1);
allList .Add(variable2);
When a String
is provided i want to search allList
and provide the result which array if were it found .
Any help archiving this in a Efficient way?
Both provided solutions run in linear time, which will be way too slow if you have lots of words and make lots of queries.
You can use a Dictionary. A Dictionary uses a hash table internally and it will be much, much faster.
To put all the strings in a dictionary, you can do:
Dictionary<String, String[]> dict = new Dictionary<String, String[]>();
foreach(String[] arr in allList)
foreach(String str in arr)
dict[str] = arr;
And then you can easily search it:
String s = "ABC";
if(dict.ContainsKey(s))
// result is dict[s]
else
// String is not in any array
Hope it helps!
You can also try this
var output=allList.Where(x=>(string.Join(",", x)+",").IndexOf(input+",")!=-1)
.FirstOrDefault();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.