{"id":8213,"date":"2024-01-28T20:00:48","date_gmt":"2024-01-28T19:00:48","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=8213"},"modified":"2024-11-19T22:25:03","modified_gmt":"2024-11-19T21:25:03","slug":"typescript-find-common-prefix","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-find-common-prefix\/","title":{"rendered":"Typescript Find Common Prefix"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"typescript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">type MyDictionary = { [key: string]: string[] };\n\nexport function groupAndFindLargest(names: string[]) {\n    if (names.length &lt;= 1) {\n        return [];\n    }\n\n    \/\/ Group names by their first two characters\n    const groupedNames = names.reduce((groups, name) => {\n        const prefix = name.slice(0, 2);\n        if (!groups[prefix]) {\n            groups[prefix] = [];\n        }\n        groups[prefix].push(name);\n        return groups;\n    }, {} as MyDictionary);\n\n    \/\/ Find the largest group\n    return Object.values(groupedNames).reduce((largest, group) => {\n        return group.length > largest.length ? group : largest;\n    }, []);\n}\n\nexport function findCommonPrefix(names: string[]) {\n    if (names.length &lt;= 1) {\n        return '';\n    }\n\n    \/\/ Sort the names to ensure that names with the same prefix are adjacent\n    const sortedNames = [...names].sort();\n\n    \/\/ Take the first and last elements (lexicographically) as reference points\n    const firstName = sortedNames[0];\n    const lastName = sortedNames[sortedNames.length - 1];\n\n    \/\/ Find the common prefix between the first and last name\n    let commonPrefix = '';\n    for (let i = 0; i &lt; firstName.length; i++) {\n        if (firstName[i] !== lastName[i]) {\n            break;\n        }\n        commonPrefix += firstName[i];\n    }\n    return commonPrefix;\n}\n\nexport function removePrefix(name: string, prefix: string) {\n    if (name.startsWith(prefix) &amp;&amp; name.length > prefix.length)\n        return name.slice(prefix.length);\n    return name;\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8213","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=8213"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8213\/revisions"}],"predecessor-version":[{"id":8215,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8213\/revisions\/8215"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=8213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=8213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=8213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}