
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Checking for uniqueness of a string in JavaScript
We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should return true if all the characters present in the string are unique. And if even one character appears for more than one, the function should return false.
We will use a hash set to keep track of the characters we encounter in the string and if at any stage of iteration, we encounter duplicate characters, we will return false otherwise at the end of iteration, we will return true.
Example
Following is the code −
const str = 'abschyie'; const checkUniqueness = (str = '') => { const hash = new Set(); for(let i = 0; i < str.length; i++){ const el = str[i]; if(hash.has(el)){ return false; }; hash.add(el); }; return true; }; console.log(checkUniqueness(str));
Output
Following is the console output −
true
- Related Articles
- Checking for uniqueness in an array in JavaScript
- Checking for string anagrams JavaScript
- Checking for permutation of a palindrome in JavaScript
- Checking for a Doubleton Number in JavaScript
- Checking for special type of Arrays in JavaScript
- Checking for squared similarly of arrays in JavaScript
- Checking for particular types of matrix in JavaScript
- Checking for Fibonacci numbers in JavaScript
- Checking for coprime numbers in JavaScript
- Checking for ascending arrays in JavaScript
- Checking for convex polygon in JavaScript
- Checking for straight lines in JavaScript
- Checking for increasing triplet in JavaScript
- Checking for special numbers in JavaScript
- Checking if a string can be made palindrome in JavaScript

Advertisements