2

I have a Array

string[] names = { "Jim Rand", "Barry Williams", "Nicole Dyne", "Peter Levitt", "Jane Jones", "Cathy Hortings"};

Is there any way to find which is the shortest(Length wise) element in this array and then store rest of elements in a different array.

Thanks, Ani

3
  • 2
    What language are you using ? Commented Apr 21, 2010 at 7:48
  • @ereOn: the syntax seems to be C#. but still, a language tag would definitely be more called for than any of the current tags. Commented Apr 21, 2010 at 7:50
  • @Nayan: Thanks but I asked before the tag was set ;) Commented Apr 21, 2010 at 9:17

3 Answers 3

9
var orderedNames = names.OrderBy(name => name.Length);

string shortestName = orderedNames.First();

string[] otherNames = orderedNames.Skip(1).ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this will be inefficient for large arrays; sorting the array requires O(n lg n) comparisons. There is an algorithm to solve this problem that is O(n). I also note that you sort the list twice in your solution. Calling First() orders the list. Calling Skip sorts the original list again. Remember, LINQ doesn't know that you haven't changed "names" on another thread between the calls; the answer might be different so the result has to be recomputed.
@Eric Lippert: ah yes, good point. adding .ToList() in the original assignment to orderedNames should remedy that last issue, right?
Yes, though then you have the problem that you are copying the same data twice; once to a list that is only useful for its first element, and then to the shorter array.
2

In C#, .Net 3.5:

string shortestName = names.Aggregate((n1, n2)=>n1.Length<n2.Length?n1:n2);

This is how you can store other elements in other array

var otherArrays = names.Exclude(new List<string>(){shortestName});

3 Comments

+1. I love how C# made such a thing so simple ;) However you're missing the second part of the question which demands to store the others elements in an array.
that gives you the length of the shortest item, tho; it doesn't tell you which item that is
Thanks a lot, I am new never tried this code..I ll try...Thanks again
1

There is no .Exclude method (or extension method) for an Array and he didn’t say that he wanted to change the collection type for the new Array. Your use of the .Aggregate is outstanding so let’s take it one step further and use .Aggregate to do the excluding as well!

Like this:

string shortestName = names.Aggregate((n1, n2) => n1.Length < n2.Length ? n1 : n2);
string nonArrayString = names.Aggregate((n1, n2) => n2 != shortestName ? n1 + " " + n2 : n1);
string[] newNames = nonArrayString.Split(' ');

David Hedlund’s technique is still far better because it’s easier to read! There are no bonus points for writing the most complicated answer... lol

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.