Unique characters in a string in javascript assignment expert

Filter Unique Characters

Given a string

myString as an input, write a JS program to find the unique characters from the myString.

Quick Tip

You can use the array method indexOf[].

  • The input will be a single line containing a string myString
  • The output should be a single line containing an array of unique characters

Sample Input 1

Was it a cat I saw?

Sample Output 1

[

'W', 'a', 's', ' ',

'i', 't', 'c', 'I',

'w', '?'

]

Sample Input 2

I did, did I?

Sample Output 2

[ 'I', ' ', 'd', 'i', ',', '?' ]

the out put should appeared same as shown in sample output's

/* This is a javaScript question, and I will also send the code with function that have to complete :-*/

"use strict";

process.stdin.resume[];

process.stdin.setEncoding["utf-8"];

let inputString = "";

let currentLine = 0;

process.stdin.on["data", [inputStdin] => {

 inputString += inputStdin;

}];

process.stdin.on["end", [_] => {

 inputString = inputString.trim[].split["\n"].map[[str] => str.trim[]];

 main[];

}];

function readLine[] {

 return inputString[currentLine++];

}

/* Please do not modify anything above this line */

function main[] {

 const myString = readLine[];

/* complete thefunction */

}

Please help me.

Filter Unique Characters

Given a string

myString as an input, write a JS program to find the unique characters from the myString.

Quick Tip

You can use the array method indexOf[].

Input

  • The input will be a single line containing a string myString

Output

  • The output should be a single line containing an array of unique characters

Sample Input 1

Was it a cat I saw?

Sample Output 1

[

'W', 'a', 's', ' ',

'i', 't', 'c', 'I',

'w', '?'

]

Sample Input 2

I did, did I?

Sample Output 2

[ 'I', ' ', 'd', 'i', ',', '?' ]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The purpose of this article is to find unique characters in a string using JavaScript.

    Example:

    Input:  Geeksforgeeks
    Output: Geksforg
    
    Input:  Geeksforgeeks is a great site for computer science
    Output: Geksforg Iaticmpun

    To achieve this, we have the following approaches:

    Approach 1: This is a naive approach to find the unique characters from a string. In this approach, we create a variable name uniq and we are iterating over the string using the for loop and at every iteration, we are checking if the uniq contains the character.

    Javascript

    function findUnique[str]{

      let uniq = "";

      for[let i = 0; i < str.length; i++]{

        if[uniq.includes[str[i]] === false]{

          uniq += str[i]

        }

      }

      return uniq;

    }

    console.log[findUnique["Geeksforgeeks"]]

    console.log[findUnique["Geeksforgeeks Is a great site for computer science"]]

    Output:

    "Geksforg"
    
    "Geksforg Iaticmpun"

    Approach 2: In this method, we use the set data structure. The set data structure contains only unique values, and we take the advantage of it. So to extract the unique values from string using Set we follow the steps below.

    • Using the split[]method convert the string into an array.
    • Create a Set using new Set[] and pass the converted array into it.
    • Now convert the set into an array using spread operator e.g:[…set]
    • And then join that array to make a string.

    Javascript

    function findUnique[str]{

      str = str.split[""]

      str = new Set[str];

      str = [...str].join[""];

      return str;

    }

    console.log[findUnique["Geeksforgeeks"]]

    console.log[findUnique["Geeksforgeeks Is a great site for computer science"]]

    Output:

    "Geksforg"
    
    "Geksforg Iaticmpun"

    Approach 3: In this approach first, we convert the string into an array using the spread operator e.g. […str] and then we apply the reduce method on that array. 

    Javascript

    function findUnique[str]{

      return [...str].reduce[[acc, curr]=>{

        return acc.includes[curr] ?  acc  :  acc + curr;

      }, ""]

    }

    console.log[findUnique["Geeksforgeeks"]]

    console.log[findUnique["Geeksforgeeks Is a great site for computer science"]]

    Output:

    "Geksforg"
    "Geksforg Iaticmpun"

    Approach-4: In this approach we will use a for-loop in order to iterate over the complete string and then by using the indexOf[] method we will check the index of each and every character [using charAt[] method] of the string which is either repeated or not repeated and then in an empty string [which we will declare initially] we will store all the unique characters.

    Javascript

    let uniqueCharacters = [stringg] => {

      let unique_characters = "";

      for [let i = 0; i < stringg.length; i++] {

        if [unique_characters.indexOf[stringg.charAt[i]] < 0] {

          unique_characters += stringg.charAt[i];

        }

      }

      return unique_characters;

    };

    console.log[uniqueCharacters["Geeksforgeeks"]];

    Output:

    Geksforg

    How do I find a unique character in a string?

    Using the toCharArray[] method The toCharArray[] method of the String class converts the given String into an array of characters and returns it. Convert it into an array of characters. Compare each character in the array with the required one. In case of a /match the String contains the required character.

    What is unique character in JavaScript?

    At each character, we check if our object contains the character and if it does, we return false . Otherwise, we add the character to the object and continue to the next index. At the end, we return true . Once we get through the whole string, we know that each character is unique and we can return true .

    How do you count the number of occurrences of all characters in a string in JavaScript?

    In the above example, a regular expression [regex] is used to find the occurrence of a string..
    const re = new RegExp[letter, 'g']; creates a regular expression..
    The match[] method returns an array containing all the matches. Here, str. ... .
    The length property gives the length of an array element..

    How do you print a character in JavaScript?

    If you are using JavaScript in the context of an HTML document and you want to print characters in th HTML content, then the simplest way is to use character references like 𐐀 [which denotes the Unicode character at code point 10400 hexadecimal] and assign the string to the innerHTML property of an element.

    Chủ Đề