{"id":478,"date":"2025-06-29T20:54:49","date_gmt":"2025-06-29T15:24:49","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=478"},"modified":"2025-06-29T20:54:50","modified_gmt":"2025-06-29T15:24:50","slug":"majority-element-ii-leetcode-229","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/","title":{"rendered":"Majority Element II | Leetcode 229 | Explained in Python"},"content":{"rendered":"\n<p>You\u2019re given an integer array <code>nums<\/code> of length <code>n<\/code>.<br>An element is called a <strong>majority element II<\/strong> if it appears <strong>strictly more than \u230a n \/ 3 \u230b times<\/strong>.<br>Because <code>n \/ 3 \u2248 33 %<\/code>, at <strong>most two<\/strong> such elements can exist.<br>Return them <strong>in any order<\/strong>.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/majority-element-ii\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Input<\/th><th>Explanation<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td><code>[3,2,3]<\/code><\/td><td><code>3<\/code> appears 2 &gt; 3\/3 = 1 times<\/td><td><code>[3]<\/code><\/td><\/tr><tr><td><code>[1,1,1,3,3,2,2,2]<\/code><\/td><td><code>1<\/code> &amp; <code>2<\/code> each appear 3 &gt; 8\/3 = 2 times<\/td><td><code>[1,2]<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Typical interview follow-ups:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Show why more than two majority-II elements are impossible.<\/li>\n\n\n\n<li>Discuss the Boyer-Moore extension that keeps <strong>two<\/strong> running candidates.<\/li>\n<\/ul>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-cf16f28b-60a5-4371-be1a-436d0ec25d49\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#0-brute-force-solution-double-triple-loop\" style=\"\">Brute Force Solution (Double \/ Triple Loop)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#1-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#4-dry-run-input-323-\" style=\"\">Dry Run (Input [3,2,3])<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#5-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#7-better-solution-hash-map-counting\" style=\"\">Better Solution (Hash-Map Counting)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#8-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#10-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#11-dry-run-input-11133222-\" style=\"\">Dry Run (Input [1,1,1,3,3,2,2,2])<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#12-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#13-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#14-optimal-solution-extended-boyer%E2%80%93moore-voting\" style=\"\">Optimal Solution (Extended Boyer\u2013Moore Voting)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#15-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#16-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#17-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#18-dry-run-input-11133222-\" style=\"\">Dry Run (Input [1,1,1,3,3,2,2,2])<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#19-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#20-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/majority-element-ii-leetcode-229\/#21-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-solution-double-triple-loop\">Brute Force Solution (Double \/ Triple Loop)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-amp-approach\">Intuition &amp; Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For every element, count its total occurrences using an inner loop.<\/li>\n\n\n\n<li>If the count exceeds <code>n \/\/ 3<\/code> and it\u2019s not already recorded, add it to the answer.<\/li>\n\n\n\n<li>Stop once two elements are found (the maximum possible).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def majorityElement(self, nums: List[int]) -&gt; List[int]:\n        n = len(nums)\n        result = []\n        for i in range(0, n):\n            # skip counting if nums[i] already confirmed\n            if len(result) == 0 or result[0] != nums[i]:\n                count = 0\n                for j in range(0, n):         # count how many nums[i]\n                    if nums[j] == nums[i]:\n                        count += 1\n                if count &gt; n \/\/ 3:             # majority-II check\n                    result.append(nums[i])\n            if len(result) == 2:               # early exit (max two answers)\n                break\n        return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">majorityElement<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># skip counting if nums[i] already confirmed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(result) == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> result[<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">] != nums[i]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                count = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> j <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, n):         <\/span><span style=\"color: #6A9955\"># count how many nums[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[j] == nums[i]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                        count += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> count &gt; n \/\/ <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:             <\/span><span style=\"color: #6A9955\"># majority-II check<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    result.append(nums[i])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(result) == <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:               <\/span><span style=\"color: #6A9955\"># early exit (max two answers)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Outer loop<\/strong> picks a potential candidate.<\/li>\n\n\n\n<li><strong>Inner loop<\/strong> counts its frequency.<\/li>\n\n\n\n<li>Duplicate work abounds, so runtime balloons.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run-input-323-\">Dry Run (Input <code>[3,2,3]<\/code>)<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>i = 0<\/code>, <code>nums[i]=3<\/code> \u2192 count=2 \u2192 2 > 1 \u2192 add 3.<\/li>\n\n\n\n<li><code>i = 1<\/code>, <code>nums[i]=2<\/code> \u2192 count=1 \u2192 fails threshold.<\/li>\n\n\n\n<li>Done \u2192 result <code>[3]<\/code>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(n\u00b2)<\/code> \u2013 nested scan.<\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(1)<\/code> extra.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h3>\n\n\n\n<p>Simple but painfully slow; unsuitable for anything but toy inputs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-better-solution-hash-map-counting\">Better Solution (Hash-Map Counting)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-amp-approach\">Intuition &amp; Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>One pass<\/strong> builds a frequency map <code>num \u2192 count<\/code>.<\/li>\n\n\n\n<li>Second pass (or inline check) collects keys whose count > <code>n \/\/ 3<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def majorityElement(self, nums: List[int]) -&gt; List[int]:\n        n = len(nums)\n        freq = {}\n        for num in nums:                       # frequency table\n            freq[num] = freq.get(num, 0) + 1\n\n        result = []\n        for k, v in freq.items():              # collect &gt; n\/\/3 keys\n            if v &gt; n \/\/ 3:\n                result.append(k)\n            if len(result) == 2:\n                break                          # at most two answers\n        return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">majorityElement<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        freq = {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> num <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> nums:                       <\/span><span style=\"color: #6A9955\"># frequency table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            freq[num] = freq.get(num, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">) + <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> k, v <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> freq.items():              <\/span><span style=\"color: #6A9955\"># collect &gt; n\/\/3 keys<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> v &gt; n \/\/ <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                result.append(k)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(result) == <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">break<\/span><span style=\"color: #D4D4D4\">                          <\/span><span style=\"color: #6A9955\"># at most two answers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dictionary insertions\/updates are <code>O(1)<\/code> amortized.<\/li>\n\n\n\n<li>Memory usage is proportional to the number of distinct elements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run-input-11133222-\">Dry Run (Input <code>[1,1,1,3,3,2,2,2]<\/code>)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>freq={1:3, 3:2, 2:3}<\/code><\/li>\n\n\n\n<li>Threshold <code>= 2<\/code> \u2192 collect <code>1, 2<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(n)<\/code><\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(k)<\/code> \u2013 <code>k<\/code> distinct numbers.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-conclusion\">Conclusion<\/h3>\n\n\n\n<p>Linear time and clear, but still needs a hash-map. We can do <strong>constant space<\/strong> next.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-optimal-solution-extended-boyer%E2%80%93moore-voting\">Optimal Solution (Extended Boyer\u2013Moore Voting)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-intuition-amp-approach\">Intuition &amp; Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>There can be <strong>\u2264 2<\/strong> majority-II elements.<\/li>\n\n\n\n<li>Maintain <strong>two candidates<\/strong> (<code>cand1<\/code>, <code>cand2<\/code>) with counters (<code>cnt1<\/code>, <code>cnt2<\/code>).<\/li>\n\n\n\n<li><strong>Cancellation rules<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If current number equals one candidate \u2192 its counter ++.<\/li>\n\n\n\n<li>Else if a counter is 0 \u2192 adopt current number as new candidate.<\/li>\n\n\n\n<li>Else \u2192 decrement both counters (pairwise cancel).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>A final verification pass counts how many times each candidate truly appears.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def majorityElement(self, nums: List[int]) -&gt; List[int]:\n        n = len(nums)\n        cand1, cand2 = None, None   # potential majorities\n        cnt1, cnt2 = 0, 0           # their vote balances\n\n        # Voting pass\n        for num in nums:\n            if cand1 == num:\n                cnt1 += 1\n            elif cand2 == num:\n                cnt2 += 1\n            elif cnt1 == 0:\n                cand1, cnt1 = num, 1\n            elif cnt2 == 0:\n                cand2, cnt2 = num, 1\n            else:                   # pairwise cancellation\n                cnt1 -= 1\n                cnt2 -= 1\n\n        # Verification pass\n        cnt1 = cnt2 = 0\n        for num in nums:\n            if num == cand1:\n                cnt1 += 1\n            elif num == cand2:\n                cnt2 += 1\n\n        result = []\n        if cnt1 &gt; n \/\/ 3:\n            result.append(cand1)\n        if cnt2 &gt; n \/\/ 3:\n            result.append(cand2)\n        return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">majorityElement<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        cand1, cand2 = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #6A9955\"># potential majorities<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        cnt1, cnt2 = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">           <\/span><span style=\"color: #6A9955\"># their vote balances<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Voting pass<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> num <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> nums:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> cand1 == num:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt1 += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> cand2 == num:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt2 += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> cnt1 == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cand1, cnt1 = num, <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> cnt2 == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cand2, cnt2 = num, <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:                   <\/span><span style=\"color: #6A9955\"># pairwise cancellation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt1 -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt2 -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Verification pass<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        cnt1 = cnt2 = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> num <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> nums:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num == cand1:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt1 += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> num == cand2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt2 += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> cnt1 &gt; n \/\/ <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            result.append(cand1)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> cnt2 &gt; n \/\/ <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            result.append(cand2)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"17-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Voting phase<\/strong> ensures that any non-majority elements are canceled out.<\/li>\n\n\n\n<li>Because a real majority-II cannot be completely canceled, it remains as <code>cand1<\/code> or <code>cand2<\/code>.<\/li>\n\n\n\n<li><strong>Verification phase<\/strong> guards against false positives (e.g., when no element exceeds n\/3).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-dry-run-input-11133222-\">Dry Run (Input <code>[1,1,1,3,3,2,2,2]<\/code>)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Step<\/th><th>num<\/th><th>cand1 \/ cnt1<\/th><th>cand2 \/ cnt2<\/th><\/tr><\/thead><tbody><tr><td>\u2026<\/td><td>\u2026<\/td><td>eventually <code>cand1=1,cnt1=1<\/code><\/td><td><code>cand2=2,cnt2=1<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Verification counts: <code>1\u21923<\/code>, <code>2\u21923<\/code> \u2192 both &gt;<code>8\/\/3 = 2<\/code> \u2192 <code>[1,2]<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"19-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(n)<\/code> \u2013 two linear scans.<\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(1)<\/code> \u2013 constant extra variables.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"20-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The extended Boyer\u2013Moore algorithm nails the sweet spot: <strong>one pass to elect candidates, one pass to confirm<\/strong>, and only a handful of variables. This is the answer interviewers look for when they mention \u201cmore than n\/3 occurrences.\u201d<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Solution<\/th><th>Time<\/th><th>Space<\/th><th>Pros<\/th><th>Cons<\/th><\/tr><\/thead><tbody><tr><td>Brute<\/td><td><code>O(n\u00b2)<\/code><\/td><td><code>O(1)<\/code><\/td><td>Conceptually simple<\/td><td>Too slow<\/td><\/tr><tr><td>Hash-Map<\/td><td><code>O(n)<\/code><\/td><td><code>O(k)<\/code><\/td><td>Easy linear pass<\/td><td>Extra memory<\/td><\/tr><tr><td>Boyer\u2013Moore II<\/td><td><code>O(n)<\/code><\/td><td><code>O(1)<\/code><\/td><td>Optimal <strong>in both<\/strong> time &amp; space<\/td><td>Slightly trickier logic<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Remember: whenever the problem says \u201cmore than n \/ k times,\u201d think <strong>Boyer\u2013Moore generalized to k \u2212 1 candidates<\/strong>. Master that pattern and majority problems become a breeze.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You\u2019re given an integer array nums of length n.An element is called a majority element II if it appears strictly more than \u230a n \/ 3 \u230b times.Because n \/ 3 \u2248 33 %, at most two such elements can exist.Return them in any order. Here&#8217;s the [Problem Link] to begin with. Example Input Explanation<\/p>\n","protected":false},"author":1,"featured_media":481,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,5],"tags":[7,18],"class_list":{"0":"post-478","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-array","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/majority-element-2-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":4,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions\/484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/481"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}