Skip to content

Commit 43e871d

Browse files
committed
adding majority element
1 parent ecd137a commit 43e871d

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def getRemovableIndices(str1, str2):
2+
if len(str1) != len(str2) + 1:
3+
return [-1]
4+
else:
5+
indices = []
6+
7+
for i in range(len(str1)):
8+
if str1[:i] + str1[i+1:] == str2:
9+
indices.append(i)
10+
11+
return indices if indices else [-1]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def majorityElement(self, nums: List[int]) -> int:
6+
7+
dic_answer = dict()
8+
len_nums = len(nums)
9+
10+
for i in range(len_nums):
11+
12+
if nums[i] not in dic_answer:
13+
dic_answer[nums[i]] = 1
14+
else:
15+
dic_answer[nums[i]] += 1
16+
17+
if dic_answer[nums[i]] > len_nums//2:
18+
return nums[i]
19+
20+
return -1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_17\
3+
.majority_element import Solution
4+
5+
class MajorityElementTestCase(unittest.TestCase):
6+
7+
def test_is_major_element(self):
8+
solution = Solution()
9+
output = solution.majorityElement(nums=[3,2,3])
10+
target = 3
11+
self.assertEqual(output, target)
12+
13+
def test_is_no_major_element(self):
14+
solution = Solution()
15+
output = solution.majorityElement(nums = [1,2,3])
16+
target = -1
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)