Friday, June 18, 2021
Home coding Write a program to find duplicate character in string.

Write a program to find duplicate character in string.

Given a number we have to calculate the square root of number and print the resultant value .

So what is square root of number?

Square root of a number is

Example:

input:abcdaab
output:a b

Logic:

step 1-make a hash array to count the character in a string.
step 2-iterate the whole string and increment hash array value .
step 3-iterate the array and print the value which is present more than once

Code-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
  int hash[26]={0};
    for(int i=0;i<s.length();i++)
    {
     hash[s[i]-'a']++;  
    }
 for(int i=0;i<26;i++)
 
{
    if(hash[i]>1)
    {
        cout<<char(i+'a')<<endl;
    }
}
   
    
}

Output:

a
b

Time complexity:0(N)

Space Complexity:O(N)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments