Click here to Skip to main content
15,847,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string array called Samples where only a finite set of string values can occur - "Charles" "Ken" "Chris" etc. There are only 11 possible string values but the number and values of the strings are unknown at the start. There are millions of entries in the array and I would like to have the values of the finite list of 11 names.

Would I create a list, e.g. NamesList, iterate through the array Samples and add to this list if NamesList.Find reveals no results for the current Samples name?

I would also like to dynamically assign a value to each of these names using another array of identical size to Samples called PositionUpdates which contains an integer value update of the position of the corresponding name in the Names array. In the end I would simply want to know the most up to date integer value of the integer position for each name as I traverse the PositionUpdates array with a for loop.

Thank you.

What I have tried:

So far I have a string array called Samples and an integer array called PositionUpdates. I can iterate through each array.
Updated 19-Jan-23 0:41am
Graeme_Grant 19-Jan-23 7:13am    
If OriginalGriff's is not what you are looking for, then, so we can better understand, please update your question with a sample set of each, say 20 items in each.
LF FF 19-Jan-23 8:30am    
Sample = { "Bill", "John", "John", "Bill", "Lee", "Bill", "Flora", "Flora", "Erin".....continues millions of times };

PositionUpdates = {1, 545, 43, 3, 3, 3, 7, 100, 9 ....identical size as Sample }

Each Sample/Position Update Pair represents an update in position (represented as an integer) for the corresponding name in the Sample
PIEBALDconsult 19-Jan-23 20:00pm    
I'd shove then into a Dictionary<string,int>
BillWoodruff 19-Jan-23 22:27pm    
how much memory do you have to work with, and, how did you end up with such a gigantic (not ordered) array ? what is the real world scenario, if any ?

"the number and values of the strings are unknown:" in an array of strings, each item is a string, and is the "value." confused.
BillWoodruff 20-Jan-23 7:54am    
What level are you at with Linq and IEnumerable ? imho, that's important in responding to you.

Would another example, using simpler Linq, be helpful ?

1 solution

Do you mean you want the indexes of each occurence, then perhaps this?
string[] lots = new string[] { "one", "two", "three", "six", "six", "two", "four", "five", "six" };
var x = lots.Select((s, i) => new { Index = i, str = s })
            .GroupBy(i => i.str)
            .Select(g => new {Str = g.Key, 
                              Indexes = g.ToList() });
Share this answer
BillWoodruff 20-Jan-23 2:14am    
+5 that code is like seeing notes by Bach for music he heard in his head (when he was deaf) ... and, when realized/played, was beautiful :)

the IGrouping of Anonymous Types it produces is a very strange beast; most mortals will have to struggle to enumerate/dereference it into "usable" form.

i wonder if the OP would benefit from a simpler version ?
OriginalGriff 20-Jan-23 3:49am    
I thought that was the simple version! :D
The alternative is a manual loop, probably involving a Dictionary<string, int> and a test / add body. Which does the same thing, but in more code!
BillWoodruff 20-Jan-23 7:51am    
I asked the OP what level he's at with Linq and IEnumerable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900