การแสดงอักขระที่ไม่ซ้ำในสตริงเพียงครั้งเดียว

ฉันมีสตริงที่มีตัวอักษรซ้ำกัน ฉันต้องการให้ตัวอักษรที่ซ้ำกันมากกว่าหนึ่งครั้งเพื่อให้แสดงเพียงครั้งเดียว ตัวอย่างเช่น ฉันมีสตริง aaabbbccc ฉันต้องการให้ผลลัพธ์เป็น abc จนถึงตอนนี้ฟังก์ชั่นของฉันทำงานดังนี้:

  • ถ้าตัวอักษรไม่ซ้ำก็ไม่แสดง
  • หากทำซ้ำหนึ่งครั้งจะแสดงเพียงครั้งเดียว (เช่น aa แสดง a)
  • หากทำซ้ำสองครั้ง แสดงทั้งหมด (เช่น aaa แสดง aaa)
  • ถ้าซ้ำ 3 ครั้งก็แสดง 6 (ถ้า aaaa แสดง aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

ฟังก์ชันจะต้องอยู่ในลูปภายในลูป นั่นเป็นเหตุผลว่าทำไม for ตัวที่สองจึงอยู่ภายในตัวแรก


person Zlatko Soleniq    schedule 13.12.2012    source แหล่งที่มา
comment
ใน regexp คุณสามารถเขียน 'aaabbbccc'.replace(/(.)\1+/g, "$1")   -  person Neil    schedule 14.12.2012
comment
ผลลัพธ์ของ unique_char('abracadabra') ควรเป็นอย่างไร?   -  person Neil    schedule 14.12.2012
comment
ผลลัพธ์ควรเป็น abrcd   -  person Zlatko Soleniq    schedule 14.12.2012
comment
@ZlatkoSoleniq: ยังไงล่ะ? มันไม่ตรงกับคำอธิบายของคุณ ว่าแต่ กรณีนี้มีประโยชน์อะไร (หรือเป็นการบ้าน)?   -  person Bergi    schedule 14.12.2012


คำตอบ (13)


กรอก Set ด้วยอักขระและต่อข้อมูลที่ไม่ซ้ำกัน:

function makeUnique(str) {
  return String.prototype.concat(...new Set(str))
}

console.log(makeUnique('abc'));    // "abc"
console.log(makeUnique('abcabc')); // "abc"

person le_m    schedule 20.06.2016
comment
ต้องใส่ต้นแบบในเส้นทางฟังก์ชัน String เพื่อให้มันทำงานได้ เจ๋งมาก! String.prototype.concat(...new Set(str)) - person Austin Haws; 22.11.2016

แปลงเป็นอาร์เรย์ก่อน จากนั้นใช้คำตอบที่นี่ แล้วเข้าร่วมอีกครั้ง ดังนี้:

var nonUnique = "ababdefegg";
var unique = nonUnique.split('').filter(function(item, i, ar){ return ar.indexOf(item) === i; }).join('');

ทั้งหมดในบรรทัดเดียว :-)

person Malcolm Holmes    schedule 01.03.2015

อาจสายเกินไป แต่ก็ยังเป็นคำตอบของฉันสำหรับโพสต์นี้:

function extractUniqCharacters(str){
    var temp = {};
    for(var oindex=0;oindex<str.length;oindex++){
        temp[str.charAt(oindex)] = 0; //Assign any value
    }
    return Object.keys(temp).join("");
}
person Parthasarathy K    schedule 27.07.2018

คุณสามารถใช้ นิพจน์ทั่วไป กับ ฟังก์ชันการแทนที่แบบกำหนดเอง:

function unique_char(string) {
    return string.replace(/(.)\1*/g, function(sequence, char) {
         if (sequence.length == 1) // if the letter doesn't repeat
             return ""; // its not shown
         if (sequence.length == 2) // if its repeated once
             return char; // its show only once (if aa shows a)
         if (sequence.length == 3) // if its repeated twice
             return sequence; // shows all(if aaa shows aaa)
         if (sequence.length == 4) // if its repeated 3 times
             return Array(7).join(char); // it shows 6( if aaaa shows aaaaaa)
         // else ???
         return sequence;
    });
}
person Bergi    schedule 13.12.2012

การใช้ lodash:

_.uniq('aaabbbccc').join(''); // gives 'abc'
person Lukasz Wiktor    schedule 14.11.2016

ตามคำถามจริง: "ถ้าจดหมายไม่ซ้ำก็ไม่แสดง"

function unique_char(str)
{
    var obj = new Object();

    for (var i = 0; i < str.length; i++)
    {
        var chr = str[i];
        if (chr in obj)
        {
            obj[chr] += 1;
        }
        else
        {
            obj[chr] = 1;
        }
    }

    var multiples = [];
    for (key in obj)
    {
        // Remove this test if you just want unique chars
        // But still keep the multiples.push(key)
        if (obj[key] > 1)
        {
            multiples.push(key);
        }
    }

    return multiples.join("");
}

var str = "aaabbbccc";
document.write(unique_char(str));
person brian buck    schedule 13.12.2012

ปัญหาของคุณคือคุณกำลังเพิ่ม unique ทุกครั้งที่คุณพบอักขระใน string จริงๆ แล้วคุณควรทำอะไรแบบนี้ (เนื่องจากคุณระบุคำตอบจะต้องซ้อนกันสำหรับลูป):

function unique_char(string){

    var str_length=string.length;
    var unique='';

    for(var i=0; i<str_length; i++){

        var foundIt = false;
        for(var j=0; j<unique.length; j++){

            if(string[i]==unique[j]){

                foundIt = true;
                break;
            }

        }

        if(!foundIt){
            unique+=string[i];
        }

    }

   return unique;
}

document.write( unique_char('aaabbbccc'))

ในที่นี้ เราจะเพิ่มเฉพาะอักขระที่พบใน string ถึง unique หากยังไม่มีอยู่ นี่ไม่ใช่วิธีที่มีประสิทธิภาพจริงๆ ในการทำเช่นนี้ ... แต่ควรใช้งานได้ตามความต้องการของคุณ

ฉันไม่สามารถเรียกใช้สิ่งนี้ได้เนื่องจากฉันไม่มีสิ่งใดที่มีประโยชน์ในการเรียกใช้ JavaScript ใน ... แต่ทฤษฎีในวิธีนี้น่าจะได้ผล

person cottonke    schedule 13.12.2012
comment
เขาไม่ได้ใช้ C# จาวาสคริปต์ของมัน - person brian buck; 14.12.2012
comment
ความคิดเห็นของฉันยังค่อนข้างถูกต้อง ... ฉันไม่มีอะไรสะดวกที่จะเรียกใช้ JavaScript และฉันไม่คิดว่าฉันใช้อะไรที่ขึ้นอยู่กับภาษาจริงๆ - person cottonke; 14.12.2012
comment
ส่วนใหญ่ -- บรรทัดนี้จะล้มเหลว: bool foundIt = false; ควรเป็น var foundIt = false; - person brian buck; 14.12.2012
comment
มันใช้งานได้กับ var แต่ฉันมีคำถามหนึ่งว่า (!foundIT) คืออะไร และสามารถเขียนด้วยวิธีอื่นได้หรือไม่ - person Zlatko Soleniq; 14.12.2012
comment
มันแค่บอกว่าถ้าเราไม่พบอักขระในสตริง unique เราก็จำเป็นต้องเพิ่มมันเข้าไป เนื่องจากนี่เป็นครั้งแรกที่เราเจออักขระตัวนี้ - person cottonke; 14.12.2012

ลองใช้วิธีนี้หากต้องแสดงอักขระที่ซ้ำกันหนึ่งครั้ง เช่น สำหรับ i/p: aaabbbccc o/p: abc

var str="aaabbbccc";
Array.prototype.map.call(str, 
  (obj,i)=>{
    if(str.indexOf(obj,i+1)==-1 ){
     return obj;
    }
  }
).join("");
//output: "abc"

และลองทำสิ่งนี้หากต้องแสดงเฉพาะอักขระที่ไม่ซ้ำเท่านั้น (String Bombarding Algo) เพิ่มเงื่อนไข "และ" อื่นเพื่อลบอักขระที่มามากกว่าหนึ่งครั้งและแสดงเฉพาะอักขระที่ไม่ซ้ำเท่านั้น เช่น สำหรับ i/p: aabbbkaha o/p: ค

var str="aabbbkaha";
Array.prototype.map.call(str, 
 (obj,i)=>{
   if(str.indexOf(obj,i+1)==-1 && str.lastIndexOf(obj,i-1)==-1){ // another and condition
     return obj;
   }
 }
).join("");
//output: "kh"
person jayad aadrit    schedule 17.06.2017

นี่คือฟังก์ชันที่ง่ายที่สุดในการทำเช่นนั้น

  function remove(text) 
    {
      var unique= "";
      for(var i = 0; i < text.length; i++)
      {
        if(unique.indexOf(text.charAt(i)) < 0) 
        {
          unique += text.charAt(i);
        }
      }
      return unique;
    }
person Ahmad Adel    schedule 01.07.2017

วิธีแก้ปัญหาบรรทัดเดียวคือใช้ Set const chars = [...new Set(s.split(''))];

person Delon    schedule 07.01.2020

นี่คือฟังก์ชันที่ง่ายที่สุดในการทำ pt นั้น 2

const showUniqChars = (text) => {
  let uniqChars = "";

  for (const char of text) {
    if (!uniqChars.includes(char))
      uniqChars += char;
  }
  return uniqChars;
};
person Ayrat Isakov    schedule 20.01.2020

const countUnique = (s1, s2) => new Set(s1 + s2).size

วิธีที่สั้นกว่าตามคำตอบ @le_m

person alex_martin    schedule 20.10.2020

person    schedule
comment
คุณช่วยกรุณาให้คำอธิบายสั้น ๆ ของโค้ดและเหตุใดจึงเป็นวิธีแก้ปัญหานี้ - person crizzis; 23.06.2017