[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 200

# Efficient Group Processing

This should be easy and want to try to get another way of doing this. I will not show my answer because I don't want to taint your way of thinking. I want to find an easy but efficient way to solve this issue.

In Parameters (2)

String[] s = {"KJDKF","SDJFK","JENN","HDEEJ","JSKE","UAJS"}
4 = Max items I want in each dimension

string[] result = Splitter(s,4);

Tho expected output:

result [0] = "KJDKF","SDJFK","JENN","HDEEJ"
result [1] = "JSKE","UAJS"

I currently have it going through 2 loops but I think there is a quicker way and my brain now hurts from other login I was doing.

Thanks

0
• 7
• 5
• 3
1 Solution

Commented:
not tested, but here's how I would do it...
``````public string[][] Splitter(string[] s, int maxNum) {

int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);
int remainder = numElements % maxNum;

List<string[]> result = new List<string[]>(numResultElements);

for (int i = 0; i < numResultElements; i++) {
string[] currArray;

int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;

currArray = new string[numCurrElements];
Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);

}

return result.ToArray();

}
``````
0

Commented:
Sorry, ignore the above - try this:
``````public string[] Splitter(string[] s, int maxNum) {

int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);
int remainder = numElements % maxNum;

List<string> result = new List<string>(numResultElements);

for (int i = 0; i < numResultElements; i++) {
string[] currArray;

int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;

currArray = new string[numCurrElements];
Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);

}

return result.ToArray();

}
``````
0

Commented:
It's quite interestin but I came to a nearly same solution as rob263. Independently :)

My is checked.
``````    private void button1_Click(object sender, EventArgs e)

{

////HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.example.com/helloworld.xml");
////string result = ((HttpWebResponse)myRequest.GetResponse()).ToString();
//XmlNodeList greeting = doc.GetElementsByTagName("greeting");
//MessageBox.Show(greeting[0].InnerXml);

//    MessageBox.Show("checked");
//}
//else{
//    MessageBox.Show("not checked");
//}

String[] s = { "KJDKF", "SDJFK", "JENN", "HDEEJ", "JSKE", "UAJS", "SDJFK", "JENN", "HDEEJ", "JSKE", "UAJS" };
int limit = 4;

List<string[]> splitted = splitter(s,limit);
}

private List<string[]> splitter(string[] origArray, int limit)
{
List<string[]> splittedArrays = new List<string[]>();
string[] currentArray = null;

for (int i = 0; i < origArray.Length; i++)
{
if ((i % limit) == 0)
{
if ((origArray.Length - limit * splittedArrays.Count) >= limit)
{
currentArray = new string[limit];
}
else
{
currentArray = new string[origArray.Length - limit * splittedArrays.Count];
}

}
currentArray[i - (splittedArrays.Count - 1) * limit] = origArray[i];
}

return splittedArrays;
}
``````
0

Commented:
Sorry! Ignore this HTTP commented part - just some rubbish :)
0

Author Commented:
Thats great, I will post one tomorrow that was suggested to me. The concept made since after I stepped back and then it hit my "why didn't I think of that" hopefully it will work.

Thanks for the input, will check them out tomorrow.
0

Author Commented:
This is what I came up with ... Any other people want to submit an example?

``````
private static string[] SplitString(string sInput, int maxItemsInArray)
{
List<string> items = new List<string>();
string[] splitter = sInput.Split(",".ToCharArray());
int pos = 0;
int group = maxItemsInArray;

while (pos < splitter.Length)
{
if (pos < splitter.Length - maxItemsInArray && group < splitter.Length)
{
StringBuilder sbVal = new StringBuilder();
for (int i = 0; i < maxItemsInArray; i++)
{
sbVal.Append(splitter[pos] + ",");
pos++;
}
group += 100;
}
else
{
StringBuilder sbVal = new StringBuilder();
for (int i = pos; i < splitter.Length; i++)
{
sbVal.Append(splitter[pos] + ",");
pos++;
}
}

}

return items.ToArray();
}
``````
0

Commented:
with all your use of stringbuilders and Substring(), I think your solution is less efficient.
0

Author Commented:
I would tend to agree, thanks for the feedback. It's interesting finding different ways to do the same thing.
0

Author Commented:
Here are the results that I recorded processing 247 GUIDS in groups of 50 max size:

My Code:
Start 0
Finish 7296576 ticks 2 ms
-----------------------------------
anarki_jimbel:
Start 0
Finish 7498099 ticks 2 ms
-----------------------------------
rob263:
Start 0
Finish 784833 ticks 0 ms

0

Author Commented:
Actually I needed to take the average of 5 different runs without debugging

My Code:
Start 0
Finish 15484805 ticks 5 ms
-----------------------------------
anarki_jimbel:
Start 0
Finish 6445026 ticks 2 ms
-----------------------------------
rob263:
Start 0
Finish 620564 ticks 0 ms

0

Commented:
Try the following - I think it might be even better.  The difference is that I no longer allocate a new array in every iteration of the loop.  Since memory allocation is a relatively slow operation, it will probably help.
``````public string[] Splitter(string[] s, int maxNum) {

int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);
int remainder = numElements % maxNum;
List<string> result = new List<string>(numResultElements);
string[] currArray = new string[maxNum];

for (int i = 0; i < numResultElements; i++) {
int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;

if (numCurrElements != maxNum) {
currArray = new string[numCurrElements];
}

Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);
}
return result.ToArray();
}
``````
0

Commented:
There were valid answers given.  Points should be awarded to rob263 and anarki_jimbel
0

Commented:
There were valid answers given.  Points should be awarded to rob263 and anarki_jimbel
0

Commented:
And it looks like rob's code is most efficient!
0

Commented:
I recommend http:#23686840 - since that's what was used in the author's benchmark test and is an order of magnitude faster than the other solutions.

0

## Featured Post

• 7
• 5
• 3
Tackle projects and never again get stuck behind a technical roadblock.