{"id":30391,"date":"2023-01-14T19:03:12","date_gmt":"2023-01-14T13:33:12","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/"},"modified":"2023-01-14T19:03:12","modified_gmt":"2023-01-14T13:33:12","slug":"solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/","title":{"rendered":"[Solved] With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-52883234\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"52883234\" data-parentid=\"52879973\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>A quick test shows that setting @jdweng&#8217;s purely LINQ solution 1X, a solution somewhat abusing LINQ to do indexing comes in at 7.4X and a solution using <code>for<\/code> comes in at 28.4X.<\/p>\n<p>(Ab)using LINQ with indexing:<\/p>\n<pre><code>var c = Enumerable.Range(0, source[0].Length).Select(chpos =&gt; Enumerable.Range(0, source.Length).Select(si =&gt; source[si][chpos]).Join()).ToArray();\n<\/code><\/pre>\n<p>Using nested <code>for<\/code> loops:<\/p>\n<pre><code>var sourcelen = source.Length;\nvar strlen = source[0].Length;\nvar c = new String[strlen];\nvar sb = new StringBuilder(strlen);\nfor (int chpos = 0; chpos &lt; strlen; ++chpos) {\n    for (int si = 0; si &lt; sourcelen; ++si)\n        sb.Append(source[si][chpos]);\n    c[chpos] = sb.ToString();\n    sb.Length = 0;\n}\n<\/code><\/pre>\n<p>Running some further tests, <code>Append<\/code> is a little faster then indexing the <code>StringBuilder<\/code> until there are a lot of long strings and building all the strings in parallel (sequentially) isn&#8217;t faster, and as the strings increase in length, a <code>Parallel.For<\/code> version catches up to the fastest nested <code>for<\/code> version and then surpasses it, if you have a large number (10,000) of strings. Interestingly <code>Append<\/code> is much slower than indexing in <code>Parallel<\/code>.<\/p>\n<p>Here is the build each indexed parallel version:<\/p>\n<pre><code>    var sourcelen = source.Length;\n    var strlen = source[0].Length;\n    var c = new String[strlen];\n    Parallel.For(0, strlen, chpos =&gt; {\n        var sb = new StringBuilder(sourcelen);\n        sb.Length = sourcelen;\n        for (int si = 0; si &lt; sourcelen; ++si)\n            sb[si] = source[si][chpos];\n        c[chpos] = sb.ToString();\n    });\n<\/code><\/pre>\n<p>Using this <code>Join<\/code> extension method greatly speeds up the solutions that use <code>String.Join(String.Empty,<\/code> or <code>String.Join(\"\",<\/code>:<\/p>\n<pre><code>public static string Join(this IEnumerable&lt;char&gt; src) {\n    var sb = new StringBuilder();\n    foreach (var c in src)\n        sb.Append(c);\n    return sb.ToString();\n}\n<\/code><\/pre>\n<p>Some notes on timings: Using LINQPad, I wrote some code to generate a sample <code>source<\/code>:<\/p>\n<pre><code>var lens = 10000;\nvar source = new string[10000];\n\/\/\n{\n    var s = Enumerable.Range(0,26).Select(letternum =&gt; new String(Convert.ToChar('a'+letternum), lens)).ToArray();\n    for (int j1 = 0; j1 &lt; source.Length; ++j1)\n        source[j1] = s[j1 % 26].ToString();\n}\n<\/code><\/pre>\n<p>Then, using LINQPad&#8217;s <code>Util.ElapsedTime<\/code>, I measured the time to process <code>source<\/code> with various implementations:<\/p>\n<pre><code>TimeSpan basetime;\n\/\/\n{\n    var start = Util.ElapsedTime;\n    var c = source.Select(x =&gt; x.Select((y, i) =&gt; new { chr = y, index = i })).SelectMany(x =&gt; x).GroupBy(x =&gt; x.index).Select(x =&gt; x.Select(y =&gt; y.chr).Join()).ToArray();\n    basetime = Util.ElapsedTime - start;\n    basetime.Dump(\"Elapsed LINQ My Join 1X\");\n    \/\/c.Dump();\n}\n\/\/\n{\n    var start = Util.ElapsedTime;\n    var sourcelen = source.Length;\n    var strlen = source[0].Length;\n    var c = new String[strlen];\n    Parallel.For(0, strlen, chpos =&gt; {\n        var sb = new StringBuilder(sourcelen);\n        sb.Length = sourcelen;\n        for (int si = 0; si &lt; sourcelen; ++si)\n            sb[si] = source[si][chpos];\n        c[chpos] = sb.ToString();\n    });\n    var myt = Util.ElapsedTime - start;\n    myt.Dump($\"Elapsed build each indexed parallel {basetime.TotalSeconds \/ myt.TotalSeconds:0.0}X\");\n    c.Dump();\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] A quick test shows that setting @jdweng&#8217;s purely LINQ solution 1X, a solution somewhat abusing LINQ to do indexing comes in at 7.4X and a solution using for comes in at 28.4X. (Ab)using LINQ with indexing: var c = Enumerable.Range(0, source[0].Length).Select(chpos =&gt; Enumerable.Range(0, source.Length).Select(si =&gt; source[si][chpos]).Join()).ToArray(); Using nested for loops: var sourcelen = source.Length; &#8230; <a title=\"[Solved] With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\" aria-label=\"More on [Solved] With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[361,324,578],"class_list":["post-30391","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","tag-c","tag-linq"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] With an array of strings of equal length, get the nth character of each string in it&#039;s own array using LINQ - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] With an array of strings of equal length, get the nth character of each string in it&#039;s own array using LINQ - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] A quick test shows that setting @jdweng&#8217;s purely LINQ solution 1X, a solution somewhat abusing LINQ to do indexing comes in at 7.4X and a solution using for comes in at 28.4X. (Ab)using LINQ with indexing: var c = Enumerable.Range(0, source[0].Length).Select(chpos =&gt; Enumerable.Range(0, source.Length).Select(si =&gt; source[si][chpos]).Join()).ToArray(); Using nested for loops: var sourcelen = source.Length; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-14T13:33:12+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ\",\"datePublished\":\"2023-01-14T13:33:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\"},\"wordCount\":200,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"arrays\",\"c++\",\"linq\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\",\"name\":\"[Solved] With an array of strings of equal length, get the nth character of each string in it's own array using LINQ - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-14T13:33:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] With an array of strings of equal length, get the nth character of each string in it's own array using LINQ - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] With an array of strings of equal length, get the nth character of each string in it's own array using LINQ - JassWeb","og_description":"[ad_1] A quick test shows that setting @jdweng&#8217;s purely LINQ solution 1X, a solution somewhat abusing LINQ to do indexing comes in at 7.4X and a solution using for comes in at 28.4X. (Ab)using LINQ with indexing: var c = Enumerable.Range(0, source[0].Length).Select(chpos =&gt; Enumerable.Range(0, source.Length).Select(si =&gt; source[si][chpos]).Join()).ToArray(); Using nested for loops: var sourcelen = source.Length; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/","og_site_name":"JassWeb","article_published_time":"2023-01-14T13:33:12+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ","datePublished":"2023-01-14T13:33:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/"},"wordCount":200,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","c++","linq"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/","url":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/","name":"[Solved] With an array of strings of equal length, get the nth character of each string in it's own array using LINQ - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-14T13:33:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-with-an-array-of-strings-of-equal-length-get-the-nth-character-of-each-string-in-its-own-array-using-linq\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] With an array of strings of equal length, get the nth character of each string in it&#8217;s own array using LINQ"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=30391"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30391\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=30391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=30391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=30391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}