{"id":345,"date":"2023-01-14T17:18:51","date_gmt":"2023-01-14T17:18:51","guid":{"rendered":"https:\/\/premsvmm.com\/?p=345"},"modified":"2023-01-14T17:18:51","modified_gmt":"2023-01-14T17:18:51","slug":"sort-array-input-based-on-sequence","status":"publish","type":"post","link":"https:\/\/premsvmm.com\/index.php\/2023\/01\/14\/sort-array-input-based-on-sequence\/","title":{"rendered":"Sort Array Input based On Sequence"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">Given an array of input, print the value based on the sequence of occurrence<\/pre>\n\n\n\n<p>Input : \u00a0 [0,1,2,3,0,0,2,2,3,0]<\/p>\n\n\n\n<p>Output: [0,0,0,0,1,2,2,2,3,3]<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Code:<\/h5>\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.*;\nimport java.util.stream.Collectors;\n\npublic class SequenceOfNumbers {\n\n    private static void sequenceOfCount(Integer[] input) {\n        Map&lt;Integer, Integer&gt; countMap = new HashMap&lt;&gt;();\n        for (Integer value : input) {\n            if (countMap.containsKey(value)) {\n                int count = countMap.get(value);\n                countMap.put(value, ++count);\n            } else {\n                countMap.put(value, 1);\n            }\n        }\n\n        Map&lt;Integer, Integer&gt; sortedMap = countMap.entrySet().stream()\n                .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))\n                .sorted(Map.Entry.comparingByKey())\n                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -&gt; e1, LinkedHashMap::new));\n        \n\n        List&lt;Integer&gt; result = new LinkedList&lt;&gt;();\n        for(Map.Entry&lt;Integer,Integer&gt; value :sortedMap.entrySet()){\n            for(int i=0;i&lt;value.getValue();i++){\n                result.add(value.getKey());\n            }\n        }\n\n        System.out.println(result);\n    }\n\n    public static void main(String[] args) {\n        sequenceOfCount(new Integer[]{0, 1, 2, 3, 0, 0, 2, 2, 3, 0}); \/\/0, 0, 0, 0, 1, 2, 2, 2, 3, 3\n        sequenceOfCount(new Integer[]{1,2,3,1,}); \/\/ 1 1 2 3\n        sequenceOfCount(new Integer[]{-1,1,-1,1}); \/\/ -1 -1 1 1\n        sequenceOfCount(new Integer[]{4,3,2,1}); \/\/ 1 2 3 4\n    }\n}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of input, print the value based on the sequence of occurrence Input : \u00a0 [0,1,2,3,0,0,2,2,3,0] Output: [0,0,0,0,1,2,2,2,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\/345"}],"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=345"}],"version-history":[{"count":1,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":346,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/345\/revisions\/346"}],"wp:attachment":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}