Determine if a string has all Unique Characters
Last Updated :
23 Jul, 2025
Given a string, determine if the string has all unique characters.
Examples :
Input : abcd10jk
Output : true
Input : hutg9mnd!nk9
Output : false
Approach 1 - Brute Force technique: Run 2 loops with variable i and j. Compare str[i] and str[j]. If they become equal at any point, return false.
C++
// C++ program to illustrate string
// with unique characters using
// brute force technique
#include <bits/stdc++.h>
using namespace std;
bool uniqueCharacters(string str)
{
// If at any time we encounter 2
// same characters, return false
for (int i = 0; i < str.length() - 1; i++) {
for (int j = i + 1; j < str.length(); j++) {
if (str[i] == str[j]) {
return false;
}
}
}
// If no duplicate characters encountered,
// return true
return true;
}
// driver code
int main()
{
string str = "GeeksforGeeks";
if (uniqueCharacters(str)) {
cout << "The String " << str
<< " has all unique characters\n";
}
else {
cout << "The String " << str
<< " has duplicate characters\n";
}
return 0;
}
// This code is contributed by Divyam Madaan
Java
// Java program to illustrate string with
// unique characters using brute force technique
import java.util.*;
class GfG {
boolean uniqueCharacters(String str)
{
// If at any time we encounter 2 same
// characters, return false
for (int i = 0; i < str.length(); i++)
for (int j = i + 1; j < str.length(); j++)
if (str.charAt(i) == str.charAt(j))
return false;
// If no duplicate characters encountered,
// return true
return true;
}
public static void main(String args[])
{
GfG obj = new GfG();
String input = "GeeksforGeeks";
if (obj.uniqueCharacters(input))
System.out.println("The String " + input + " has all unique characters");
else
System.out.println("The String " + input + " has duplicate characters");
}
}
Python3
# Python program to illustrate string
# with unique characters using
# brute force technique
def uniqueCharacters(str):
# If at any time we encounter 2
# same characters, return false
for i in range(len(str)):
for j in range(i + 1,len(str)):
if(str[i] == str[j]):
return False;
# If no duplicate characters
# encountered, return true
return True;
# Driver Code
str = "GeeksforGeeks";
if(uniqueCharacters(str)):
print("The String ", str," has all unique characters");
else:
print("The String ", str, " has duplicate characters");
# This code contributed by PrinciRaj1992
C#
// C# program to illustrate string with
// unique characters using brute force
// technique
using System;
public class GFG {
static bool uniqueCharacters(String str)
{
// If at any time we encounter 2
// same characters, return false
for (int i = 0; i < str.Length; i++)
for (int j = i + 1; j < str.Length; j++)
if (str[i] == str[j])
return false;
// If no duplicate characters
// encountered, return true
return true;
}
public static void Main()
{
string input = "GeeksforGeeks";
if (uniqueCharacters(input) == true)
Console.WriteLine("The String " + input
+ " has all unique characters");
else
Console.WriteLine("The String " + input
+ " has duplicate characters");
}
}
// This code is contributed by shiv_bhakt.
PHP
<?php
// PHP program to illustrate string
// with unique characters using
// brute force technique
function uniqueCharacters($str)
{
// If at any time we encounter 2
// same characters, return false
for($i = 0; $i < strlen($str); $i++)
{
for($j = $i + 1; $j < strlen($str); $j++)
{
if($str[$i] == $str[$j])
{
return false;
}
}
}
// If no duplicate characters
// encountered, return true
return true;
}
// Driver Code
$str = "GeeksforGeeks";
if(uniqueCharacters($str))
{
echo "The String ", $str,
" has all unique characters\n";
}
else
{
echo "The String ", $str,
" has duplicate characters\n";
}
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to illustrate string with
// unique characters using brute force
// technique
function uniqueCharacters(str)
{
// If at any time we encounter 2
// same characters, return false
for(let i = 0; i < str.length; i++)
for(let j = i + 1; j < str.length; j++)
if (str[i] == str[j])
return false;
// If no duplicate characters
// encountered, return true
return true;
}
// Driver code
let input = "GeeksforGeeks";
if (uniqueCharacters(input) == true)
document.write("The String " + input +
" has all unique characters" + "</br>");
else
document.write("The String " + input +
" has duplicate characters");
// This code is contributed by decode2207
</script>
Output :
The String GeeksforGeeks has duplicate characters
Time Complexity: O(n2)
Auxiliary Space: O(1)
Note: Please note that the program is case-sensitive.
Approach 2 - Sorting: Using sorting based on ASCII values of characters
C++
// C++ program to illustrate string
// with unique characters using
// brute force technique
#include <bits/stdc++.h>
using namespace std;
bool uniqueCharacters(string str)
{
// Using sorting
sort(str.begin(), str.end());
for (int i = 0; i < str.length()-1; i++) {
// if at any time, 2 adjacent
// elements become equal,
// return false
if (str[i] == str[i + 1]) {
return false;
}
}
return true;
}
// driver code
int main()
{
string str = "GeeksforGeeks";
if (uniqueCharacters(str)) {
cout << "The String " << str
<< " has all unique characters\n";
}
else {
cout << "The String " << str
<< " has duplicate characters\n";
}
return 0;
}
// This code is contributed by Divyam Madaan
Java
// Java program to check string with unique
// characters using sorting technique
import java.util.*;
class GfG {
/* Convert the string to character array
for sorting */
boolean uniqueCharacters(String str)
{
char[] chArray = str.toCharArray();
// Using sorting
// Arrays.sort() uses binarySort in the background
// for non-primitives which is of O(nlogn) time complexity
Arrays.sort(chArray);
for (int i = 0; i < chArray.length - 1; i++) {
// if the adjacent elements are not
// equal, move to next element
if (chArray[i] != chArray[i + 1])
continue;
// if at any time, 2 adjacent elements
// become equal, return false
else
return false;
}
return true;
}
// Driver code
public static void main(String args[])
{
GfG obj = new GfG();
String input = "GeeksforGeeks";
if (obj.uniqueCharacters(input))
System.out.println("The String " + input
+ " has all unique characters");
else
System.out.println("The String " + input
+ " has duplicate characters");
}
}
Python3
# Python3 program to illustrate string
# with unique characters using
# brute force technique
def uniqueCharacters(st):
# Using sorting
st = sorted(st)
for i in range(len(st)-1):
# if at any time, 2 adjacent
# elements become equal,
# return false
if (st[i] == st[i + 1]) :
return False
return True
# Driver code
if __name__=='__main__':
st = "GeeksforGeeks"
if (uniqueCharacters(st)) :
print("The String",st,"has all unique characters\n")
else :
print("The String",st,"has duplicate characters\n")
# This code is contributed by AbhiThakur
C#
// C# program to check string with unique
// characters using sorting technique
using System;
public class GFG {
/* Convert the string to character array
for sorting */
static bool uniqueCharacters(String str)
{
char[] chArray = str.ToCharArray();
// Using sorting
Array.Sort(chArray);
for (int i = 0; i < chArray.Length - 1; i++) {
// if the adjacent elements are not
// equal, move to next element
if (chArray[i] != chArray[i + 1])
continue;
// if at any time, 2 adjacent elements
// become equal, return false
else
return false;
}
return true;
}
// Driver code
public static void Main()
{
string input = "GeeksforGeeks";
if (uniqueCharacters(input) == true)
Console.WriteLine("The String " + input
+ " has all unique characters");
else
Console.WriteLine("The String " + input
+ " has duplicate characters");
}
}
// This code is contributed by shiv_bhakt.
JavaScript
<script>
// Javascript program to
// check string with unique
// characters using sorting technique
/* Convert the string to character array
for sorting */
function uniqueCharacters(str)
{
let chArray = str.split('');
// Using sorting
chArray.sort();
for (let i = 0; i < chArray.length - 1; i++)
{
// if the adjacent elements are not
// equal, move to next element
if (chArray[i] != chArray[i + 1])
continue;
// if at any time, 2 adjacent elements
// become equal, return false
else
return false;
}
return true;
}
let input = "GeeksforGeeks";
if (uniqueCharacters(input) == true)
document.write("The String " + input +
" has all unique characters" + "</br>");
else
document.write("The String " + input +
" has duplicate characters" + "</br>");
</script>
Output:
The String GeeksforGeeks has duplicate characters
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Approach 3 - Use of Extra Data Structure: This approach assumes ASCII char set(8 bits). The idea is to maintain a boolean array for the characters. The 256 indices represent 256 characters. All the array elements are initially set to false. As we iterate over the string, set true at the index equal to the int value of the character. If at any time, we encounter that the array value is already true, it means the character with that int value is repeated.
C++
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_CHAR = 256;
bool uniqueCharacters(string str)
{
// If length is greater than 265,
// some characters must have been repeated
if (str.length() > MAX_CHAR)
return false;
bool chars[MAX_CHAR] = { 0 };
for (int i = 0; i < str.length(); i++) {
if (chars[int(str[i])] == true)
return false;
chars[int(str[i])] = true;
}
return true;
}
// driver code
int main()
{
string str = "GeeksforGeeks";
if (uniqueCharacters(str)) {
cout << "The String " << str
<< " has all unique characters\n";
}
else {
cout << "The String " << str
<< " has duplicate characters\n";
}
return 0;
}
// This code is contributed by Divyam Madaan
Java
// Java program to illustrate String With
// Unique Characters using data structure
import java.util.*;
class GfG {
int MAX_CHAR = 256;
boolean uniqueCharacters(String str)
{
// If length is greater than 256,
// some characters must have been repeated
if (str.length() > MAX_CHAR)
return false;
boolean[] chars = new boolean[MAX_CHAR];
Arrays.fill(chars, false);
for (int i = 0; i < str.length(); i++) {
int index = (int)str.charAt(i);
/* If the value is already true, string
has duplicate characters, return false */
if (chars[index] == true)
return false;
chars[index] = true;
}
/* No duplicates encountered, return true */
return true;
}
// Driver code
public static void main(String args[])
{
GfG obj = new GfG();
String input = "GeeksforGeeks";
if (obj.uniqueCharacters(input))
System.out.println("The String " + input
+ " has all unique characters");
else
System.out.println("The String " + input
+ " has duplicate characters");
}
}
Python3
# Python program to illustrate
# string with unique characters
# using data structure
MAX_CHAR = 256;
def uniqueCharacters(string):
n = len(string)
# If length is greater than 256,
# some characters must have
# been repeated
if n > MAX_CHAR:
return False
chars = [False] * MAX_CHAR
for i in range(n):
index = ord(string[i])
'''
* If the value is already True,
string has duplicate characters,
return False'''
if (chars[index] == True):
return False
chars[index] = True
''' No duplicates encountered,
return True '''
return True
# Driver code
if __name__ == '__main__':
input = "GeeksforGeeks"
if (uniqueCharacters(input)):
print("The String", input,
"has all unique characters")
else:
print("The String", input,
"has duplicate characters")
# This code is contributed by shikhasingrajput
C#
// C# program to illustrate String With
// Unique Characters using data structure
using System;
class GfG {
static int MAX_CHAR = 256;
bool uniqueCharacters(String str)
{
// If length is greater than 256,
// some characters must have been repeated
if (str.Length > MAX_CHAR)
return false;
bool[] chars = new bool[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++) {
chars[i] = false;
}
for (int i = 0; i < str.Length; i++) {
int index = (int)str[i];
/* If the value is already true, string
has duplicate characters, return false */
if (chars[index] == true)
return false;
chars[index] = true;
}
/* No duplicates encountered, return true */
return true;
}
// Driver code
public static void Main(String[] args)
{
GfG obj = new GfG();
String input = "GeeksforGeeks";
if (obj.uniqueCharacters(input))
Console.WriteLine("The String " + input
+ " has all unique characters");
else
Console.WriteLine("The String " + input
+ " has duplicate characters");
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to illustrate String With
// Unique Characters using data structure
let MAX_CHAR = 256;
function uniqueCharacters(str)
{
// If length is greater than 256,
// some characters must have been repeated
if (str.length > MAX_CHAR)
return false;
let chars = new Array(MAX_CHAR);
for (let i = 0; i < MAX_CHAR; i++) {
chars[i] = false;
}
for (let i = 0; i < str.length; i++) {
let index = str[i].charCodeAt();
/* If the value is already true, string
has duplicate characters, return false */
if (chars[index] == true)
return false;
chars[index] = true;
}
/* No duplicates encountered, return true */
return true;
}
let input = "GeeksforGeeks";
if (uniqueCharacters(input))
document.write("The String " + input
+ " has all unique characters");
else
document.write("The String " + input
+ " has duplicate characters");
</script>
Output:
The String GeeksforGeeks has duplicate characters
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach 4 - Without Extra Data Structure: The approach is valid for strings having alphabet as a-z. This approach is a little tricky. Instead of maintaining a boolean array, we maintain an integer value called checker(32 bits). As we iterate over the string, we find the int value of the character with respect to 'a' with the statement int bitAtIndex = str.charAt(i)-'a';
Then the bit at that int value is set to 1 with the statement 1 << bitAtIndex .
Now, if this bit is already set in the checker, the bit AND operation would make the checker > 0. Return false in this case.
Else Update checker to make the bit 1 at that index with the statement checker = checker | (1 <<bitAtIndex);
C++
// C++ program to illustrate string
// with unique characters using
// brute force technique
#include <bits/stdc++.h>
using namespace std;
bool uniqueCharacters(string str)
{
// Assuming string can have characters
// a-z, this has 32 bits set to 0
int checker = 0;
for (int i = 0; i < str.length(); i++) {
int bitAtIndex = str[i] - 'a';
// if that bit is already set in
// checker, return false
if ((checker & (1 << bitAtIndex)) > 0) {
return false;
}
// otherwise update and continue by
// setting that bit in the checker
checker = checker | (1 << bitAtIndex);
}
// no duplicates encountered, return true
return true;
}
// driver code
int main()
{
string str = "geeksforgeeks";
if (uniqueCharacters(str)) {
cout << "The String " << str
<< " has all unique characters\n";
}
else {
cout << "The String " << str
<< " has duplicate characters\n";
}
return 0;
}
// This code is contributed by Divyam Madaan
Java
// Java program to illustrate String with unique
// characters without using any data structure
import java.util.*;
class GfG {
boolean uniqueCharacters(String str)
{
// Assuming string can have characters a-z
// this has 32 bits set to 0
int checker = 0;
for (int i = 0; i < str.length(); i++) {
int bitAtIndex = str.charAt(i) - 'a';
// if that bit is already set in checker,
// return false
if ((checker & (1 << bitAtIndex)) > 0)
return false;
// otherwise update and continue by
// setting that bit in the checker
checker = checker | (1 << bitAtIndex);
}
// no duplicates encountered, return true
return true;
}
// Driver Code
public static void main(String args[])
{
GfG obj = new GfG();
String input = "geekforgeeks";
if (obj.uniqueCharacters(input))
System.out.println("The String " + input
+ " has all unique characters");
else
System.out.println("The String " + input
+ " has duplicate characters");
}
}
Python3
# Python3 program to illustrate String with unique
# characters without using any data structure
import math
def uniqueCharacters(string):
# Assuming string can have characters
# a-z this has 32 bits set to 0
checker = 0
for i in range(len(string)):
bitAtIndex = ord(string[i]) - ord('a')
# If that bit is already set in
# checker, return False
if ((bitAtIndex) > 0):
if ((checker & ((1 << bitAtIndex))) > 0):
return False
# Otherwise update and continue by
# setting that bit in the checker
checker = checker | (1 << bitAtIndex)
# No duplicates encountered, return True
return True
# Driver Code
if __name__ == '__main__':
input = "geekforgeeks"
if (uniqueCharacters(input)):
print("The String " + input +
" has all unique characters")
else:
print("The String " + input +
" has duplicate characters")
# This code is contributed by Princi Singh
C#
// C# program to illustrate String
// with unique characters without
// using any data structure
using System;
class GFG {
public virtual bool uniqueCharacters(string str)
{
// Assuming string can have
// characters a-z this has
// 32 bits set to 0
int checker = 0;
for (int i = 0; i < str.Length; i++) {
int bitAtIndex = str[i] - 'a';
// if that bit is already set
// in checker, return false
if ((checker & (1 << bitAtIndex)) > 0) {
return false;
}
// otherwise update and continue by
// setting that bit in the checker
checker = checker | (1 << bitAtIndex);
}
// no duplicates encountered,
// return true
return true;
}
// Driver Code
public static void Main(string[] args)
{
GFG obj = new GFG();
string input = "geekforgeeks";
if (obj.uniqueCharacters(input)) {
Console.WriteLine("The String " + input + " has all unique characters");
}
else {
Console.WriteLine("The String " + input + " has duplicate characters");
}
}
}
// This code is contributed by Shrikant13
PHP
<?php
// PHP program to illustrate
// string with unique characters
// using brute force technique
function uniqueCharacters($str)
{
// Assuming string can have
// characters a-z, this has
// 32 bits set to 0
$checker = 0;
for ($i = 0; $i < strlen($str); $i++)
{
$bitAtIndex = $str[$i] - 'a';
// if that bit is already set
// in checker, return false
if (($checker &
(1 << $bitAtIndex)) > 0)
{
return false;
}
// otherwise update and continue by
// setting that bit in the checker
$checker = $checker |
(1 << $bitAtIndex);
}
// no duplicates encountered,
// return true
return true;
}
// Driver Code
$str = "geeksforgeeks";
if(uniqueCharacters($str))
{
echo "The String ", $str,
" has all unique characters\n";
}
else
{
echo "The String ", $str,
" has duplicate characters\n";
}
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to illustrate String
// with unique characters without
// using any data structure
function uniqueCharacters(str)
{
// Assuming string can have
// characters a-z this has
// 32 bits set to 0
let checker = 0;
for (let i = 0; i < str.length; i++) {
let bitAtIndex = str[i].charCodeAt(0) - 'a'.charCodeAt(0);
// if that bit is already set
// in checker, return false
if ((checker & (1 << bitAtIndex)) > 0) {
return false;
}
// otherwise update and continue by
// setting that bit in the checker
checker = checker | (1 << bitAtIndex);
}
// no duplicates encountered,
// return true
return true;
}
let input = "geekforgeeks";
if (uniqueCharacters(input)) {
document.write("The String " + input + " has all unique characters");
}
else {
document.write("The String " + input + " has duplicate characters");
}
</script>
Output :
The String GeekforGeeks has duplicate characters
Time Complexity: O(n)
Auxiliary Space: O(1)
Exercise: Above program is case insensitive, you can try making the same program that is case sensitive i.e Geeks and GEeks both give different output.
Using Java Stream :
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
bool uniqueCharacters(string s)
{
// If at any character more than once create another
// stream stream count more than 0, return false
for (int i = 0; i < s.size(); i++) {
for (int j = i + 1; j < s.size(); j++) {
if (s[i] == s[j]) {
return false;
}
}
}
return true;
}
int main()
{
string input = "GeeksforGeeks";
if (uniqueCharacters(input))
cout << "The String " << input
<< " has all unique characters\n";
else
cout << "The String " << input
<< " has duplicate characters\n";
return 0;
}
// This code is contributed by phasing17
Java
import java.util.Collections;
import java.util.stream.Collectors;
class GfG {
boolean uniqueCharacters(String s)
{
// If at any character more than once create another stream
// stream count more than 0, return false
return s.chars().filter(e-> Collections.frequency(s.chars().boxed().collect(Collectors.toList()), e) > 1).count() > 1 ? false: true;
}
public static void main(String args[])
{
GfG obj = new GfG();
String input = "GeeksforGeeks";
if (obj.uniqueCharacters(input))
System.out.println("The String " + input + " has all unique characters");
else
System.out.println("The String " + input + " has duplicate characters");
}
}
//Write Java code here
Python3
# Python3 program to implement the approach
from collections import Counter
def uniqueCharacters(s):
# If at any character more than once create another stream
# stream count more than 0, return false
return not any(filter(lambda x: x > 1, list(Counter(list(s)).values())))
# Driver code
input = "GeeksforGeeks"
if uniqueCharacters(input):
print("The String " + input + " has all unique characters")
else:
print("The String " + input + " has duplicate characters")
# This code is contributed by phasing17
C#
// C# program to implement the approach
using System.Linq;
class GfG {
public bool UniqueCharacters(string s) {
// If at any character more than once create another stream
// stream count more than 0, return false
return s.ToCharArray().Count(e => s.Count(f => f == e) > 1) > 1 ? false : true;
}
// Driver Code
static void Main(string[] args) {
GfG obj = new GfG();
string input = "GeeksforGeeks";
if (obj.UniqueCharacters(input))
System.Console.WriteLine("The String " + input + " has all unique characters");
else
System.Console.WriteLine("The String " + input + " has duplicate characters");
}
}
// This code is contributed by Prasad Kandekar(prasad264)
JavaScript
// JavaScript code to implement the approach
function uniqueCharacters(s)
{
// If at any character more than once create another
// stream stream count more than 0, return false
let arr = s.split("");
return !arr.some((v, i) => arr.indexOf(v) < i);
}
let input = "GeeksforGeeks";
if (uniqueCharacters(input))
console.log("The String " + input + " has all unique characters");
else
console.log("The String " + input + " has duplicate characters");
// This code is contributed by phasing17
Reference:
Cracking the Coding Interview by Gayle
Approach 5: Using sets() function:
- Convert the string to set.
- If the length of set is equal to the length of the string then return True else False.
Below is the implementation of the above approach
C++
// C++ program to illustrate String with unique
// characters using set data structure
#include <bits/stdc++.h>
using namespace std;
bool uniqueCharacters(string str)
{
set<char> char_set;
// Inserting character of string into set
for(char c : str)
{
char_set.insert(c);
}
// If length of set is equal to len of string
// then it will have unique characters
return char_set.size() == str.size();
}
// Driver code
int main()
{
string str = "GeeksforGeeks";
if (uniqueCharacters(str))
{
cout << "The String " << str
<< " has all unique characters\n";
}
else
{
cout << "The String " << str
<< " has duplicate characters\n";
}
return 0;
}
// This code is contributed by abhishekjha558498
Java
// Java program to illustrate String with unique
// characters using set data structure
import java.util.*;
class GFG{
static boolean uniqueCharacters(String str)
{
HashSet<Character> char_set = new HashSet<>();
// Inserting character of String into set
for(int c = 0; c< str.length();c++)
{
char_set.add(str.charAt(c));
}
// If length of set is equal to len of String
// then it will have unique characters
return char_set.size() == str.length();
}
// Driver code
public static void main(String[] args)
{
String str = "GeeksforGeeks";
if (uniqueCharacters(str))
{
System.out.print("The String " + str
+ " has all unique characters\n");
}
else
{
System.out.print("The String " + str
+ " has duplicate characters\n");
}
}
}
// This code contributed by umadevi9616
Python3
# Python3 program to illustrate String with unique
# characters
def uniqueCharacters(str):
# Converting string to set
setstring = set(str)
# If length of set is equal to len of string
# then it will have unique characters
if(len(setstring) == len(str)):
return True
return False
# Driver Code
if __name__ == '__main__':
input = "GeeksforGeeks"
if (uniqueCharacters(input)):
print("The String " + input +
" has all unique characters")
else:
print("The String " + input +
" has duplicate characters")
# This code is contributed by vikkycirus
C#
// C# program to illustrate String with unique
// characters using set data structure
using System;
using System.Collections.Generic;
public class GFG {
static bool uniquechars(String str)
{
HashSet<char> char_set = new HashSet<char>();
// Inserting character of String into set
for (int c = 0; c < str.Length; c++) {
char_set.Add(str[c]);
}
// If length of set is equal to len of String
// then it will have unique characters
if (char_set.Count == str.Length) {
return true;
}
else {
return false;
}
}
// Driver code
public static void Main(String[] args)
{
String str = "GeeksforGeeks";
if (uniquechars(str)) {
Console.Write("The String " + str
+ " has all unique characters\n");
}
else {
Console.Write("The String " + str
+ " has duplicate characters\n");
}
}
}
// This code is contributed by umadevi9616
JavaScript
<script>
// Function program to illustrate String
// with unique characters
function uniqueCharacters(str)
{
// Converting string to set
var setstring = new Set(str)
// If length of set is equal to len of string
// then it will have unique characters
if (setstring.size == str.length)
{
return true
}
else
{
return false
}
}
// Driver Code
var input = "GeeksforGeeks"
if (uniqueCharacters(input))
{
document.write("The String " + input +
" has all unique characters") ;
}
else
{
document.write("The String " + input +
" has duplicate characters")
}
// This code is contributed by bunnyram19
</script>
Output:
The String GeeksforGeeks has duplicate characters
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem