Logo

dev-resources.site

for different kinds of informations.

Write a function that removes duplicate characters from a given string. ( Try to write core JS)

Published at
8/15/2024
Categories
javascript
string
array
duplicate
Author
ajaymarathe
Categories
4 categories in total
javascript
open
string
open
array
open
duplicate
open
Author
11 person written this
ajaymarathe
open
Write a function that removes duplicate characters from a given string. ( Try to write core JS)
const removeDuplicateChar = (str1) => {
    let newArr = [];
    let stringArr = str1.split("");
    for (let i = 0; i < stringArr.length; i++) {
      if (!newArr.includes(stringArr[i])) {
        newArr.push(stringArr[i]);
      }
    }
    return newArr.join("");
  };

console.log("removeDuplicate:",removeDuplicateChar("helloooo"));
 // removeDuplicate: helo
Enter fullscreen mode Exit fullscreen mode

Problem Explanation

Imagine you have a word, and some of the letters in that word are repeated. What if you wanted to create a version of that word where each letter appears only once? That’s what the removeDuplicateCharfunction is for.

How It Works:

  • Breaking Down the Word: The function starts by splitting your word into individual letters. For example, if you have the word "helloooo", it breaks it down into ['h', 'e', 'l', 'l', 'o', 'o', 'o', 'o'].

  • Checking for Repeats: The function then goes through each letter one by one. It checks if that letter has already been added to a new list of letters. If the letter is new (meaning it hasn't been added yet), it adds it to this new list. If the letter has already been added, it skips it.

  • Building the Result: After going through all the letters, the function ends up with a list where each letter appears only once. For "helloooo", it would be ['h', 'e', 'l', 'o'].

  • Final Output: The function gives you this new list, which now contains only unique characters from the original word.
    Example:
    If you use the function with "helloooo", it will return ['h', 'e', 'l', 'o'], removing all the extra 'o's.

array Article's
30 articles in total
Favicon
Exploring PHP 8.4: Exciting New Features with Examples
Favicon
Leetcode β€” 3190. Find Minimum Operations to Make All Elements Divisible by Three
Favicon
Leetcode β€” Top Interview 150–121. Best Time to Buy and Sell Stock
Favicon
Leetcode β€” Top Interview 150 β€” Remove Element
Favicon
Sliding subarray beauty
Favicon
The Ultimate Guide to Arrays in Java: From Zero to Hero (With a Dash of Humor)
Favicon
The Ultimate Guide to JavaScript Array Operations πŸ–₯️
Favicon
Grid unique path
Favicon
Maximum product subarray
Favicon
Hilbert Space free will generator used by Kabbalists in sects like NASA and the CIA
Favicon
Create JS function to remove spaces from giving string. ( Using core js and not in-built trim function.)
Favicon
Write a function that removes duplicate characters from a given string. ( Try to write core JS)
Favicon
array[]: uma classe especial gerenciada internamente pela prΓ³pria JVM
Favicon
Array
Favicon
Learn Lodash _.drop - Creates a slice of array with n elements dropped from the beginning.
Favicon
#LearnedToday: Object.groupBy()
Favicon
Write a function that filters out all the falsy values from a given array. (Core JS)
Favicon
Master in array
Favicon
Methods of Array in Javascript
Favicon
The JavaScript Array Methods
Favicon
What is a WeakMap in JavaScript?
Favicon
What is a Set in JS?
Favicon
Mastering JavaScript: Deep Dive into Array Tooling.πŸš€πŸš€
Favicon
how to find the second largest number in an array
Favicon
Add Elements to JavaScript Array
Favicon
Day 3: Introduction to Arrays πŸ“‹
Favicon
Arrays in JavaScript: A Comprehensive Guide
Favicon
Day 6: Mastering Arrays in JavaScript πŸš€
Favicon
30 Essential Array Methods in JavaScript with Examples
Favicon
Copying Arrays and Objects in JavaScript Without References

Featured ones: