Sunday, 15 August 2010

javascript - How can I avoid counting triplicates as pairs while iterating through an ordered series of letters within an array? -



javascript - How can I avoid counting triplicates as pairs while iterating through an ordered series of letters within an array? -

i wrote simple programme analyze string find word greatest amount of duplicate letters within it. takes given string, breaks array of separated words, , breaks each separate word alphabetically sorted groups of individual letters (which compared prev , next, 2 @ time, containing array iterated through). 2 adjacent , matching values found adds 1 tally hash-file next word in question, , word tallied pairs of duplicate letters returned @ end greatest. no matching pairs found in word returns -1. it's supposed do.

below, i've run problem: if don't utilize regexp replace 1 of matched characters, code gives false positives count triplicates (eg, "eee"), 2 separate pairs, (eg, "eee" = "ee & ee", instead of beingness viewed "ee, e"). however, if utilize regexp below prevent triplicate counts, doing breaks loop mid-stride, , skips next word. there no way create way work? if not, improve employ regexp deletes chars except duplicate characters in question, , perhaps split .length of each word 2 number of pairs remaining? ideas how solve help.

var str = "helloo aplpplpp pie"; //var str = "no repting letrs"; //var str = "ceoderbyte"; function lettercounti(str) { var input = str.split(" "); console.log(input); console.log("\n") var hashobject = {}; var word = ""; var count = 0; for(var = 0; i<input.length; i++) { var currentitem = input[i]; var currentwordintochars = currentitem.split("").sort(); console.log(currentwordintochars); var counter = 0; for(var j=1; j<currentwordintochars.length; j++) { console.log(currentwordintochars[j-1] + "=currentchar j-1"); console.log(currentwordintochars[j] + "=prev j"); console.log("-"); var final = currentitem; if(currentwordintochars[j-1] == currentwordintochars[j]) { counter++; hashobject[final] = counter; //currentwordintochars = currentwordintochars[j-1].replace(/[a-z]/gi, string.fromcharcode(currentitem.charcodeat(0)+1)); //here replace j-1 random# or //to avoid 3 in row beingness counted 2 pair //or utilize regexp remove pairs, , //then split .length/2 pairs. console.log(counter + " === # total char pairs"); } if(count<hashobject[currentitem]) { word = final; count = hashobject[currentitem]; } } } console.log(hashobject); console.log("\n"); (var o in hashobject) if (o) homecoming word; homecoming -1; } console.log(lettercounti(str));

an other way it, consists replace duplicate characters in sorted word:

var str = "helloo aplpplpp pie"; function lettercounti(str) { var input = str.split(" "); var count = 0; var result = -1; for(var = 0; i<input.length; i++) { var nb = 0; var sorteditem = input[i].split("").sort().join(""); sorteditem.replace(/(.)\1/g, function (_) { nb++ }); if (nb > count) { count = nb; result = input[i]; } } homecoming result; } console.log(lettercounti(str));

notes: replace method way increment nb using callback function. can same using match method , counting results.

if 2 words have same number of duplicates, first word returned default. can alter behaviour status of if statement.

javascript regex

No comments:

Post a Comment