Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add majority element #1136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shivamp1998 wants to merge 10 commits into TheAlgorithms:master from shivamp1998:master
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Bit-Manipulation/BinaryCountSetBits.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

function BinaryCountSetBits (a) {
'use strict'

// check whether input is an integer, some non-integer number like, 21.1 have non-terminating binary expansions and hence their binary expansion will contain infinite ones, thus the handling of non-integers (including strings,objects etc. as it is meaningless) has been omitted

if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer')

// convert number into binary representation and return number of set bits in binary representation
return a.toString(2).split('1').length - 1
}
Expand Down
6 changes: 0 additions & 6 deletions Bit-Manipulation/test/BinaryCountSetBits.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,3 @@ test('check BinaryCountSetBits of 0 is 0', () => {
const res = BinaryCountSetBits(0)
expect(res).toBe(0)
})
test('check BinaryCountSetBits of 21.1 throws error', () => {
expect(() => BinaryCountSetBits(21.1)).toThrow()
})
test('check BinaryCountSetBits of {} throws error', () => {
expect(() => BinaryCountSetBits({})).toThrow()
})
6 changes: 0 additions & 6 deletions CONTRIBUTING.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ and inspect the outcome. Example: [RatInAMaze.test.js](Backtracking/tests/RatInA

Please refrain from using `console` in your implementation AND test code.

First you should install all dependencies using:

```shell
npm install
```

You can (and should!) run all tests locally before committing your changes:

```shell
Expand Down
4 changes: 2 additions & 2 deletions Ciphers/CaesarCipher.js → Ciphers/CaesarsCipher.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @param {number} rotation - the number of rotation, expect real number ( > 0)
* @return {string} - decrypted string
*/
const caesarCipher = (str, rotation) => {
const caesarsCipher = (str, rotation) => {
if (typeof str !== 'string' || !Number.isInteger(rotation) || rotation < 0) {
throw new TypeError('Arguments are invalid')
}
Expand All @@ -29,4 +29,4 @@ const caesarCipher = (str, rotation) => {
})
}

export default caesarCipher
export default caesarsCipher
16 changes: 0 additions & 16 deletions Ciphers/test/CaesarCipher.test.js
View file Open in desktop

This file was deleted.

16 changes: 16 additions & 0 deletions Ciphers/test/CaesarsCipher.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import caesarsCipher from '../CaesarsCipher'

describe('Testing the caesarsCipher function', () => {
it('Test - 1, Testing for invalid types', () => {
expect(() => caesarsCipher(false, 3)).toThrow()
expect(() => caesarsCipher('false', -1)).toThrow()
expect(() => caesarsCipher('true', null)).toThrow()
})

it('Test - 2, Testing for valid string and rotation', () => {
expect(caesarsCipher('middle-Outz', 2)).toBe('okffng-Qwvb')
expect(caesarsCipher('abcdefghijklmnopqrstuvwxyz', 3)).toBe('defghijklmnopqrstuvwxyzabc')
expect(caesarsCipher('Always-Look-on-the-Bright-Side-of-Life', 5)).toBe('Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj')
expect(caesarsCipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 23)).toBe('QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD')
})
})
46 changes: 46 additions & 0 deletions Data-Structures/Array/MajorityElement.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
Copy link
Collaborator

@appgurueu appgurueu Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment needs to be on the findMajorityElement function.

* [Majority Element](https://www.geeksforgeeks.org/majority-element/) A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element)
* @complexity: O(n) (on average )
* @complexity: O(n) (worst case)
* @flow
*/

function findCandidate (array) {
let indexMajority = 0
let count = 1
const size = array.length
for (let i = 1; i < size; i++) {
if (array[indexMajority] === array[i]) {
count++
} else {
count--
if (count === 0) {
indexMajority = i
count = 1
}
}
}
return array[indexMajority]
}

// verifies if candidate occurs more than size/2 times in an array
function isMajorityElement (array, candidate) {
let count = 0
const size = array.length
for (let i = 0; i < size; i++) {
if (array[i] === candidate) {
if (++count > size / 2) {
return true
}
}
}
return false
}

function findMajorityElement (array) {
// finds the candidate for majority
const cand = findCandidate(array)
return isMajorityElement(array, cand) ? cand : -1
}

export { findMajorityElement }
14 changes: 14 additions & 0 deletions Data-Structures/Array/test/MajorityElement.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { findMajorityElement } from '../MajorityElement'

describe('Majority element cases', () => {
Copy link
Collaborator

@appgurueu appgurueu Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inline your arrays for all test cases.

it('no majority element in array test', () => {
expect(findMajorityElement([1, 2, 3, 4, 5])).toEqual(-1)
})
it('One majority element present', () => {
expect(findMajorityElement([3, 3, 4, 2, 4, 4, 2, 4, 4])).toEqual(4)
})
it('Majority element when array elements are exactly equal to n/2 ', () => {
const Array = [3, 4, 2, 2, 2, 2, 8, 4]
expect(findMajorityElement(Array)).toEqual(-1)
})
})
31 changes: 0 additions & 31 deletions Maths/CircularArc.js
View file Open in desktop

This file was deleted.

19 changes: 0 additions & 19 deletions Maths/test/CircularArc.test.js
View file Open in desktop

This file was deleted.

27 changes: 0 additions & 27 deletions Project-Euler/Problem007.js
View file Open in desktop

This file was deleted.

17 changes: 0 additions & 17 deletions Project-Euler/test/Problem007.test.js
View file Open in desktop

This file was deleted.

38 changes: 0 additions & 38 deletions Search/Minesweeper.js
View file Open in desktop

This file was deleted.

8 changes: 4 additions & 4 deletions Search/TernarySearch.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* Ternary search is similar to binary search but it divides the sorted array
* into three parts and determines which part the key lies in. The array will
/* Ternary search is similar to binary search but it divide the sorted array
* into three parts and determine which part the key lies in. The array will
* be divided into three intervals by using two middle points, mid1 and mid2.
* The value of the key will first be compared with the two mid points, the value
* The value of the key will first compared with the two mid points, the value
* will be returned if there is a match. Then, if the value of the key is less
* than mid1, narrow the interval to the first part. Else, if the value of the
* key is greater than mid2, narrow the interval to the third part. Otherwise,
* narrow the interval to the middle part. Repeat the steps until the value is
* found or the interval is empty(value not found after checking all elements).
* found or the interval is empty(value not found after checking all elements).
*
* Reference: https://www.geeksforgeeks.org/ternary-search/
*/
Expand Down
75 changes: 0 additions & 75 deletions Search/test/Minesweeper.test.js
View file Open in desktop

This file was deleted.

15 changes: 0 additions & 15 deletions String/test/BoyerMoore.test.js
View file Open in desktop

This file was deleted.

Loading

AltStyle によって変換されたページ (->オリジナル) /