{"id":329,"date":"2023-01-12T13:35:32","date_gmt":"2023-01-12T13:35:32","guid":{"rendered":"https:\/\/premsvmm.com\/?p=329"},"modified":"2023-01-12T13:59:07","modified_gmt":"2023-01-12T13:59:07","slug":"sort-numbers-based-on-input-and-occurrence-count","status":"publish","type":"post","link":"https:\/\/premsvmm.com\/index.php\/2023\/01\/12\/sort-numbers-based-on-input-and-occurrence-count\/","title":{"rendered":"Sort numbers based on input and occurrence count"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">Given a array of input, sort it based on the number of&nbsp;occurrence count\n<\/pre>\n\n\n\n<div class=\"inherit-container-width wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>Input : [8,0,1,5,5,3,3,3]<\/p>\n\n\n\n<p>Output : [0,1,8,5,5,3,3,3]<\/p>\n<\/div>\n\n\n\n<p>Code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">package wordpress;\n\nimport java.util.Arrays;\nimport java.util.LinkedHashMap;\nimport java.util.Map;\nimport java.util.stream.Collectors;\n\npublic class SortNumbers {\n    public static Integer[] sortBasedOnOccurrences(Integer[] input) {\n        Map&lt;Integer, Integer&gt; occurrenceCount = new LinkedHashMap&lt;&gt;();\n        for (Integer key : input) {\n            if (occurrenceCount.containsKey(key)) {\n                int count = occurrenceCount.get(key);\n                occurrenceCount.put(key, ++count);\n            } else {\n                occurrenceCount.put(key, 1);\n            }\n        }\n\n        Map&lt;Integer, Integer&gt; sorted = occurrenceCount\n                .entrySet()\n                .stream()\n                .sorted(Map.Entry.comparingByKey())\n                .sorted(Map.Entry.comparingByValue())\n                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -&gt; e1, LinkedHashMap::new));\n\n        Integer[] result = new Integer[input.length];\n        int i = 0;\n        for (Map.Entry&lt;Integer, Integer&gt; entry : sorted.entrySet()) {\n            int counter = entry.getValue();\n            while (counter != 0) {\n                result[i++] = entry.getKey();\n                counter--;\n            }\n        }\n\n        return result;\n    }\n\n    public static void main(String[] args) {\n        System.out.println(Arrays.toString(sortBasedOnOccurrences(new Integer[]{8, 0, 1, 5, 5, 3, 3, 3}))); \/\/ 0 1 8 5 5 3 3 3\n        System.out.println(Arrays.toString(sortBasedOnOccurrences(new Integer[]{3, 3, 1, 2, 4}))); \/\/ 1 2 4 3 3\n        System.out.println(Arrays.toString(sortBasedOnOccurrences(new Integer[]{0,0,1,1,2,2}))); \/\/ 0 0 1 1 2 2\n        System.out.println(Arrays.toString(sortBasedOnOccurrences(new Integer[]{0}))); \/\/ 0\n    }\n}\n\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a array of input, sort it based on the number of&nbsp;occurrence count Input : [8,0,1,5,5,3,3,3] Output : [0,1,8,5,5,3,3,3] Code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[10],"tags":[11],"_links":{"self":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/329"}],"collection":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":3,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":336,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/329\/revisions\/336"}],"wp:attachment":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}