{"id":22513695,"url":"https://github.com/emahtab/majority-element","last_synced_at":"2026-02-11T05:02:42.941Z","repository":{"id":79525620,"uuid":"231078288","full_name":"eMahtab/majority-element","owner":"eMahtab","description":"Majority Element","archived":false,"fork":false,"pushed_at":"2019-12-31T11:50:51.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-22T12:26:47.242Z","etag":null,"topics":["leetcode","majority-element","problem-solving"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-12-31T11:30:48.000Z","updated_at":"2019-12-31T11:50:53.000Z","dependencies_parsed_at":"2023-05-10T17:15:50.137Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/majority-element","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/majority-element","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmajority-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmajority-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmajority-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmajority-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/majority-element/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmajority-element/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29327095,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T03:52:29.695Z","status":"ssl_error","status_checked_at":"2026-02-11T03:52:23.094Z","response_time":97,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","majority-element","problem-solving"],"created_at":"2024-12-07T03:14:05.115Z","updated_at":"2026-02-11T05:02:42.924Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Majority Element\n\nGiven an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.\n\nYou may assume that the array is non-empty and the majority element always exist in the array.\n\n```\nExample 1:\n\nInput: [3,2,3]\nOutput: 3\n\nExample 2:\n\nInput: [2,2,1,1,1,2,2]\nOutput: 2\n```\n\n### Implementation 1 :\n\n```java\npublic int majorityElement(int[] nums) {\n        if(nums == null || nums.length == 0)\n            return -1;\n        \n        Map\u003cInteger,Integer\u003e map = new HashMap\u003c\u003e();\n        for(int i : nums){\n            if(map.containsKey(i)){\n                map.put(i, map.get(i) + 1);\n            } else{\n                map.put(i, 1); \n            }\n        }\n        \n        int majority = (int) Math.floor(nums.length / 2);\n        for(Integer n : map.keySet()){\n            if(map.get(n) \u003e majority){\n                return n;\n            }\n        }\n        return -1;\n}\n```\nAbove implementation have runtime complexity of O(n) and space complexity of O(d), where d is the count of distinct numbers in the input array.\n\n```\nRuntime Complexity = O(n)\nSpace Complexity   = O(d)\n```\n\n### Implementation 2 :\n\n```java\npublic int majorityElement(int[] nums) {\n       if(nums == null || nums.length == 0)\n            return -1;\n            \n       Arrays.sort(nums);\n       return nums[nums.length / 2]; \n}\n```\nAbove implementation have runtime complexity of O(nlogn) and space complexity of O(1)\n\n```\nRuntime Complexity = O(nlogn)\nSpace Complexity   = O(1)\n```\n\n### Implementation 3 :\n\n```java\npublic int majorityElement(int[] nums) {\n       if(nums == null || nums.length == 0)\n            return -1;\n        \n       int result = 0, count = 0;\n       for(int i = 0; i \u003c nums.length; i++ ) {\n          if(count == 0){\n             result = nums[ i ];\n             count = 1;\n         } else if(result == nums[i]){\n           count++;\n         } else {\n           count--;\n         }\n       }\n \n     return result;\n}\n```\nAbove implementation have runtime complexity of O(n) and space complexity of O(1).\n\n```\nRuntime Complexity = O(n)\nSpace Complexity   = O(1)\n```\n\n\n## References :\nhttps://www.programcreek.com/2014/02/leetcode-majority-element-java/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmajority-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fmajority-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmajority-element/lists"}