Content-Length: 336423 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1103/files/addd3ec1d96974432df05ac90d621bd9d5f4e395

16 algorithm: first unique char in a string by 10kartik · Pull Request #1103 · TheAlgorithms/JavaScript · GitHub
Skip to content

algorithm: first unique char in a string #1103

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

Merged
merged 4 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
* [CountVowels](String/CountVowels.js)
* [CreatePermutations](String/CreatePermutations.js)
* [DiceCoefficient](String/DiceCoefficient.js)
* [FirstUniqueCharacter](String/FirstUniqueCharacter.js)
* [FormatPhoneNumber](String/FormatPhoneNumber.js)
* [GenerateGUID](String/GenerateGUID.js)
* [HammingDistance](String/HammingDistance.js)
Expand Down
21 changes: 21 additions & 0 deletions String/FirstUniqueCharacter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @function firstUniqChar
* @description Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
* @param {String} str - The input string
* @return {Number} - The index of first unique character.
* @example firstUniqChar("javascript") => 0
* @example firstUniqChar(""sesquipedalian"") => 3
* @example firstUniqChar("aabb") => -1
*/

const firstUniqChar = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Argument should be string')
}
for (let i = 0; i < str.length; ++i) {
if (str.indexOf(str[i]) === str.lastIndexOf(str[i])) return i
}
return -1
}

export { firstUniqChar }
9 changes: 9 additions & 0 deletions String/test/FirstUniqueCharacter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { firstUniqChar } from '../FirstUniqueCharacter'

describe('firstUniqChar', () => {
it('locates the index of first unique character in the string', () => {
expect(firstUniqChar('javascript')).toEqual(0)
expect(firstUniqChar('sesquipedalian')).toEqual(3)
expect(firstUniqChar('aabb')).toEqual(-1)
})
})








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1103/files/addd3ec1d96974432df05ac90d621bd9d5f4e395

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy