Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Javascript to detect repeated characters

Posted on 2003-11-20
Medium Priority
710 Views
I need a javascript routine that will detect repeated characters in an array.  The repeated characters are not necessarily next to each other in the array.
0
Question by:Barry62
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 5
• 3
• +4

LVL 10

Expert Comment

ID: 9790671
Give an example and the expected result.
0

LVL 15

Expert Comment

ID: 9790800
anu chaacter have a numeric value (as in the ascii table) the value may vary with different alphabets but each chaacte vill have its own unique value in whichever alphabet.

we can use that by creating an array where those character values are the indeces in the array:

var inputString = "you put your string into this variable"

function mulChars( str ) {       // find multiple characters in a string
var  charCount = new Array();    // here we accunulate counts for each character found
var i = str.length;
var chCode;
for (var i=str.length-1; i>=0; i-- ) {
chCode = str.charCodeAt( i );
if ( typeof( charCount[chCode] ) == 'undefined' ) {
charCount[chCode] = 1;                       // first ocurrence of that character
} else {
charCount[chCode]++                         // all other ocurrences increas the count
}
}
//  now the array is complete. for any cell in the array the content may be:
//    undefined            // char with that code was not found in the string
//    a number             // hov many times that character was found.
var result = "";
for (var i=0; i<charCount.length; i++ ) {
if ( typeof( charCount[i] ) == 'number'  &&  charCount[i] > 1 ) result += String.fromCharCode( i );
}
return result;
} // end mulChars

var res = mulChars( inputString );
alert( "The following characters occur more than once: '" +res +"'." );

mvh JakobA
0

LVL 31

Expert Comment

ID: 9790826
Here we are, easily modified to give the repeating char if you want as well

<script>
function repeats(arr){
found=false
for  (i=0;i<arr.length;i++)
for (j=i+1;j<arr.length;j++)
if (arr[i] == arr[j]) found=true
return(found)
}
chrs1=new Array("a","c","d","t","s","q")
chrs2=new Array("a","c","d","t","w","d","s")
</script>
0

LVL 31

Expert Comment

ID: 9790966
Here is the code giving the repeats as well

<script>

function repeats(arr){
found=false
reps=" "
for  (i=0;i<arr.length;i++)
for (j=i+1;j<arr.length;j++)
if (arr[i] == arr[j])
{found=true
reps+=arr[i]
}
return(found+reps)
}
chr1=new Array("a","c","z","t","w","d","s")
chr2=new Array("a","c","d","t","w","d","c","a","s")
</script>
0

LVL 15

Expert Comment

ID: 9791040
GwynforWeb >> try this:

chr1=new Array("a","c","a","t","a","d","s");         // 'a' occur 3 times

I think you wil get a recorded as a repeat twice.
0

LVL 8

Author Comment

ID: 9791080
OK, GwynforWeb,

I tried to adapt your code to my program, but it's not working.

Here's what I'm trying to do:

I'm writing a word jumble program in ASP and JavaScript.  I break up a random word(supplied by a table) and populate an ASP array with each separate letter, after randomizing them.

I display the scrambled word and then allow the user to click on each letter, which places the selected letter in the solution area.  The selected letter in the scrambled word then turns red.  If the user clicks on the red letter again, it is erased from the solution.  This works fine until I select a repeated letter.  The program then erase the letter from the solution as if it was the first time I selected it.

I can populate a JavaScript array from the displayed word, but I need a routine in Javascript that will not erase the previous occurence of the repeated letter.

Barry62
0

LVL 31

Expert Comment

ID: 9791089
When dealing with strings you can use the fiollowing simple regular expression

<script>
function repTest(s)
{return ( /(\w).*\1/.test(s) )}

s="acfds_n3"
s="acfdsn"
s="acfdsns"
s="acwwfdsnxxs"
</script>

0

LVL 8

Author Comment

ID: 9791170
Also, I need to keep track of the repeated letter I've already used.
0

LVL 31

Expert Comment

ID: 9791207
Barry62, I see what you are trying to do, to do it I must know how the words are being displayed, in a table?

0

LVL 1

Expert Comment

ID: 9791306
I know it's already been done, but I thought I'd give it a try anyway.  Here's what I came up with, it's a slightly different approach...

var arrSource = new Array('a', 'b', 'a', 'c', 'd', 'a', 'b');
var arrDupes = new Array();
var objHash = new Object();
for (var intIndex in arrSource) {
var strLetter = arrSource[intIndex];
if (strLetter in objHash) arrDupes[strLetter] = true;
else objHash[strLetter] = 1;
}

var arrDupeResults = new Array();
for (strDupe in arrDupes) arrDupeResults.push(strDupe);
0

LVL 15

Expert Comment

ID: 9791441
new attempt using an array of characters as input as specified:

// - - - - - - beginning of code

var inputArray = [ 'a', 'l', 'f', 'a', 'b', 'e', 't', ' ', 's', 'o', 'u', 'p' ];  // you set it to whatever

function mulChars( inArr ) {       // find multiple characters in a string
var outArr = new Array();   // here we accunulate counts for each character found
var ch;
for (var i=inArr.length-1; i>=0; i-- ) {
ch = str.charAt( i );
if ( outArrt[ch] ) {
outArr[ch]++                         // all other ocurrences increas the count
} else {
outArr[ch] = 1;                      // first ocurrence of that character
}
}
return outArr;
} // end mulChars

var checkArray = mulChars( inputArray );     // checkArray must be remembered

// now we use the generated checkArray to keep track of which letters can be added or should be removed:

if ( checkArray[ charStr ] ) {
if ( checkArray[ charStr ] > 0 ) {
checkArray[ charStr ]--;   //count down
return true;               // you can add it (I have already done the countdown so you better)
} else {
checkArray[ charStr ]++;   // count up
return false;              // you must remove it (I have already done the countup so you better)
}
} else {
alert( "Serious error. 'canIAddThis' called with a parameter not in the original array." );
}
}

// - - - - -  end of code

whenever the user click on a letter you call 'canIAddThis' with the chosen letter as parameter. if the function return true add the letter, if it return fals remove it,
if ( canIAddThis( "a" ) ) {
// find an uncolored version of that letter and color it (there will be one)
} else {
// remove the letter
// find an colored version of that letter and uncolor it (there will be one)
}

regards JakobA
0

LVL 8

Author Comment

ID: 9796933
The scrambled word is being displayed as plain text, each letter inside its own span container.  The choices are being displayed in readonly text controls.
0

LVL 31

Expert Comment

ID: 9801126
I can not do this properly with seeing you code but you can remove a letter in a span by setting the innerHTML to "". The code below might give you some ideas

<form name="ans">
<input type="text" name="l1" size=1>
<input type="text" name="l2" size=1>
<input type="text" name="l3" size=1>
<input type="text" name="l4" size=1>
<input type="text" name="l5" size=1>
</form><p>
<span onClick='document.ans.l2.value=this.innerHTML;this.innerHTML=""'>P</span>
<span onClick='document.ans.l1.value=this.innerHTML;this.innerHTML=""'>S</span>
<span onClick='document.ans.l3.value=this.innerHTML;this.innerHTML=""'>I</span>
<span onClick='document.ans.l5.value=this.innerHTML;this.innerHTML=""'>S</span>
<span onClick='document.ans.l4.value=this.innerHTML;this.innerHTML=""'>T</span>
0

LVL 8

Author Comment

ID: 9810788
OK, that would work fine.  However, I am already removing a letter fine when it is re-selected.  Here is my code.  You'll have to run it to see exactly what I'm talking about.

<html>
<title>Jumble</title>
<script language="JavaScript">
var corrheight=768;
var corrwidth=1024;
if(screen.width<corrwidth||screen.height<corrheight){
}
if(screen.width>=corrwidth||screen.height>=corrheight){
}
function focusit(el){
elem= document.getElementById(el);
elem.focus();
newClass(elem);
}
function newClass(elem){
elem.className='hilite';
}
function oldClass(elem){
elem.className='under';
}

function hilite(el){
elem = document.getElementById(el);
elem.className='red';
}
function checkJum(ltr,elnum,jumel){
//alert("'" + ltr + "' " + elnum)
word=new Array(elnum-1);
for(chk = 1;chk <= elnum;chk++){
el = "ltr" + chk;
elem = document.getElementById(el);
jumelement = "jum" + jumel;
jumelem = document.getElementById(jumelement);
for(x=0;x<=elnum;x++){
word[x]=elem.value;
}
//alert(ltr + " " + "'" + elem.value + "'")
if(elem.value == ""){
if(!repeats(word)){
elem.value=ltr;
jumelem.className='red';
break;
}
}
if(elem.value != ""){
if(elem.value == ltr){

elem.value="";
jumelem.className='nostyle';
break;

}
}
}
}
function checkJum2(ltr,numltrs,elnum){
for(chk=1;chk<=numltrs;chk++){
elname="jum" + chk;
elementnum=document.getElementById(elname);
ltrel="ltr" + elnum;
ltrelement = document.getElementById(ltrel);
if(ltr == elementnum.value){
elementnum.className='nostyle';
}
}
}

function repeats(arr){
found=false
for  (i=0;i<arr.length;i++)
for (j=i+1;j<arr.length;j++)
if (arr[i] == arr[j]) found=true
return(found)
}
</script>
<style>
input{background:#5B386E;border:none;border-bottom:solid #E6BE4C;color:#E6BE4C;font-size:16px;font-family:'Comic Sans MS';width:1em;text-transform:uppercase;}
.hilite{border:solid thin #E6BE4C;}
.under{border:none;border-bottom:solid #E6BE4C;cursor:hand;}
.nostyle{border:none;cursor:hand;}
.red{border:none;color:#FF0000;cursor:hand;}
</style>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" style="font-family:'Comic Sans MS';font-size:16px;">
<div align="center">
<%
connJumble.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("../db/games.mdb") & ";"
wordsSQL = "SELECT * from singles"
cmdJumble.CursorType=3
cmdJumble.open wordsSQL,connJumble
dim wordarray()
dim word1()
arrcnt = 1
Randomize()
iRandom1 = Int(cmdJumble.RecordCount * Rnd + 1)
redim wordarray(cmdJumble.RecordCount)
while not cmdJumble.eof
wordarray(arrcnt)=ucase(cmdJumble("word"))
arrcnt = arrcnt + 1
cmdJumble.MoveNext
wend
redim word1(len(wordarray(iRandom1)))
one = wordarray(iRandom1)
'scramble word1
for x1 = 1 to len(wordarray(iRandom1))
rndnum1 = Int(len(one) * Rnd + 1)
word1(x1) = mid(one,rndnum1,1)
one = mid(one,1,rndnum1-1) & mid(one,rndnum1 + 1,len(one)-rndnum1)
response.write "<span id=""jum" & x1 & """ class=""nostyle"" onClick=""checkJum('" & word1(x1) & "'," & len(wordarray(iRandom1)) & "," & x1 & ")"">" & word1(x1) & "</span>"
next
response.write "<BR><BR>"
response.write "<BR><BR>"
response.write "<form name='guess' action='jumble.asp' method='post'>"
for y1 = 1 to len(wordarray(iRandom1))
if word1(y1) <> " " then
response.write "<input type=""text"" id='ltr" & y1 & "' style=""width:1em;"" class=""under"" readonly onClick=""checkJum2('" & word1(y1) & "'," & len(wordarray(iRandom1)) & "," & y1 & ")"">&nbsp;" &  vbCrlf
else
response.write "&nbsp;&nbsp;&nbsp;"
end if
next
response.write "</form>"
%>
</div>
</body>
</html>
0

LVL 16

Expert Comment

ID: 10383199
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

jAy
EE Cleanup Volunteer
0

LVL 8

Author Comment

ID: 10383626
That's fine, thanks.
0

Accepted Solution

SpazMODic earned 0 total points
ID: 10420413
PAQed, with points refunded (250)

SpazMODic
EE Moderator
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some ofâ€¦
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is â€“ how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
If youâ€™ve ever visited a web page and noticed a cool font that you really liked the look of, but couldnâ€™t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yoâ€¦
###### Suggested Courses
Course of the Month6 days, 10 hours left to enroll