-3
A
回答
2
試試這個:
var str = "aaabcccdeffa"; // Original string
// We are going to create a key-value array to store the number of occurance
// per letter (eg. 'a' : 4, 'b' : 1 etc.)
var store = {};
// Next we loop through each letter in the string
for (var a in str) {
if (store[str[a]] == undefined) { // If this letter has not ben found before, we set the count to 1 (first occurance)
store[str[a]] = 1;
}
else { // else if the letter has been found, we increase the count by one
store[str[a]] += 1;
}
}
// At this point, we have a key value array that contains the count of each letter
// Next, we loop through this array to generate the new string
var newStr = ''; // Initialise new string
for (var char in store) {
newStr += char; // append the letter to the string
if (store[char] > 1) {
newStr += store[char]; // If the count is more than one, we want to show the number too, so we append the number to the string
}
}
輸出將在newStr
1
這裏是使用jquery和正則表達式來計算字符的頻率的參考代碼。
// Variable Declaration with Source text
var sourceText="aaabcccdeffa";
var resultText="";
// Splitting the source text to array
var sourceTextArray=sourceText.split("");
var uniqueText = [];
//Fetches Unique text from sourceTextArray in order
$.each(sourceTextArray, function(i, el){
if($.inArray(el, uniqueText) === -1) uniqueText.push(el);
});
//Iteration with unique text array
$.each(uniqueText, function(i, el){
//Regular Expression approach to calculate frequency of character with source text
resultText+=(sourceText.match(new RegExp(el, "g")) || []).length>1?el+(sourceText.match(new RegExp(el, "g")) || []).length:el;
});
alert(resultText);
2
你可以使用一個哈希表,這在javascript通過對象來完成。此代碼的作品
function duplicateCharacters(str) {
//Create an empty object
var hashTable = {};
for(var i = 0; i < str.length; i++){
//Check if the character has already been registered
//If false, register it and link a 1 to it
//If true, increment the integer linked to it
if (hashTable.hasOwnProperty(str[i]))
hashTable[str[i].toString()]++;
else
hashTable[str[i].toString()] = 1;
}
var output = "";
//Go through the hashTable
for(var key in hashTable) {
//Concatenate the key
output += key.toString();
//If the character only appeared once, do not add it
if(hashTable[key] != 1)
output += hashTable[key].toString()
}
return output;
}
相關問題
- 1. 得到第一個字符的重複值的字符串
- 2. 傑克遜的用途:在JSON字符串得到重複場
- 3. 得到一個字符串在javascript
- 4. 的Javascript得到的字符串
- 5. 在QStringList中尋找重複的字符串並獲得重複的計數
- 6. 最長子串非重複字符javascript
- 7. JavaScript for循環不重複字符串
- 8. 檢測重複字符背靠背Javascript
- 9. Javascript字符串數組NG-重複
- 10. 在Javascript中檢測字符串中的重複字母
- 11. JavaScript的查詢字符串得到
- 12. 得到重複的值在JSON對象的javascript(JQuery的)
- 13. 重複字符
- 14. 如何替換字符串中的重複字符 - Javascript
- 15. 檢查字符串中重複的字符Javascript
- 16. 在字符串中重複
- 17. 刪除字符串中的重複或重複字符
- 18. 轉換重複記錄到重複的字符串數組
- 19. 字符串的非重複字符
- 20. STEPWISE重複字符串中的字符
- 21. 字符串中最重複的字符
- 22. 返回字符串中的第一個非重複字符在javascript中
- 23. 重複字符串
- 24. 在javascript中刪除重複的字符串
- 25. 檢查字符串是在JavaScript未知串的重複
- 26. 複選框到字符串JavaScript
- 27. 得到字符
- 28. 將字符串中的重複字符移動到末尾
- 29. 找到字符串中的重複字符
- 30. 重複的字符串
你有什麼試過?什麼樣的輸入?一個字符串,一個數組,一個文本框? – Weedoze
歡迎來到StackOverflow!要獲得幫助,您應該提供[MCVE](https://stackoverflow.com/help/mcve)。這可以幫助其他人瞭解您正在嘗試做什麼,並向我們展示您已經嘗試過的內容。 – Chewtoy