473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to find unique chars count of a string in javascript?

Unique characters:

Eg: If 'unique' is a keyword, the unique characters count should be = 4
Apr 9 '10 #1
2 3561
jkmyoung
2,057 Expert 2GB
edit-- Brute force is probably the most suitable for you. If you're not interested in algorithms, ignore the rest of the post.

I suggest using a bubble sort like algorithm. If you're bubbling and one
character matches another, 'delete' that character.

Sort the first 1 character.
Then sort the first 2 characters.
Then sort the first 3 characters.

This requires keeping track of 3 index variables.
uniqueChars, compareIndex, nextCharacter

eg, take unique.

1'st iteration.
u : uniqueChars = 1
u + n => nu: uniqueChars = 2
nu + i => inu: uniqueChars = 3
inu + q => inqu: uniqueChars = 4
inuq + u => inqu: uniqueChars = 4
inqu + e => einqu: uniqueChars = 5

Once you have this working, you could use a binary search like algorithm to speed up the time.

Another example: pineapple
p
p+i -> ip
ip+n -> inp
inp + e -> einp
einp + a -> einpa
einpa + p -> einpa
einpa + p -> einpa
einpa + l -> eilnpa
eilnpa + e -> eilnpa 6.
=========
If you don't like this method you could always do the lazy method.
Apr 9 '10 #2
Dormilich
8,658 Expert Mod 8TB
is that something like a Binary Heap?
Apr 11 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: mail.zhf | last post by:
if have two char use equality, out first. example: char str = "gbdfssdffss"; out is: f.
4
by: grocery_stocker | last post by:
#include <iostream> //for cin and cout #include <iomanip> // for setw() #include <string> // for strlen() strcmp() strrev() #include <fstream> //ifstream and ofstream: file (input & output)...
2
by: SJM | last post by:
I have a report that displays records of real estate properties. It is possible for each property to appear a number of times for various reasons. Each record however is unique. What I would like...
3
by: MLH | last post by:
I have a 50-char job name field (text type) in an Access table. The entries into this field will be unique. No two records will house the same job name. I would like an algorithm that would...
9
by: tshad | last post by:
How do I find (and set) a couple of labels in the Footer after a DataGrid is filled? I have a bunch of DataGrids that get displayed nested inside a DataList. The datagrid looks like: ...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
4
by: Saso Zagoranski | last post by:
Hi, I'm making a simple application which will store different items (in a SQL server 2000 - MSDE database). Each item has a unique ID in this form: / / / an example: AZ12345/1234/1/A
2
by: roN | last post by:
Hey, I got following: <form name="Formular" method="post" onSubmit="return chkFormular()" action="mail.php" enctype="text/plain"> .... .... .... <td align="right" valign="top"...
1
by: Lars E | last post by:
Hi all I have a small problem. I have a datatable with 8 columns. But it is only data in 5 of the columns. Data for the remaing 3 columns is in another dataset. I Want to run trough the...
7
by: cj | last post by:
I need to choose a 20 char unique id for transactions handled by my multithreaded program. I can't decide if to use Dim uid As String = Guid.NewGuid.ToString uid = uid.Replace("-", "") uid =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.