JavaScript数组删除重复的单词或字符(如果仅输入字符。请勿从1个单词中删除所有重复的单词或字符

时间:2018-11-16 03:06:37

标签: javascript arrays loops continue

输入:Samsung,再次输入:Samsung,再次输入:Tomas,再次输入:sass,再次输入:sass,再次输入:b,再次输入:b,再次输入:bb,再次输入:bb .... ....

并显示以下内容: 使用removeDuplicateUsingSet:Samsung,Tomas,sass,b,bb 不带功能:Samsung,Samsung,Tomas,sass,sass,b,b,bb,bb 和键入stop时停止的循环.....

function removeDuplicateUsingSet(arr){
let unique_array = Array.from(new Set(arr))
return unique_array}

var inputs =[];
var alreadyEntered = false;
while (!alreadyEntered) 
{
var input = prompt("Enter items until you enter it twice");

for (var i=0;i<inputs.length;i++) {
    if (inputs[i] == input)
     {
        alert("Already entered!");
        alreadyEntered = true;
        break;
    }

}
inputs.push(input);
}



alert("With removeDuplicateUsingSet function : " + "\n" + 
removeDuplicateUsingSet(inputs) + "\n" + "Without: " + "\n" + inputs);

现在此代码具有中断循环,我不知道如何解决。 试图做下一件事情:

function removeDuplicateUsingSet(arr){
let unique_array = Array.from(new Set(arr))
return unique_array
}

var array = [];
var stored = [];
while(array !== 'stop')
{
stored.push(prompt('what are your fav books ? '));
array = prompt('If you would like to continue enter any key otherwise enter or type stop');

// document.write(stored + " , ");
console.log(removeDuplicateUsingSet(array) + "\n___________");
}
alert("With removeDuplicateUsingSet function : " + "\n" + 
removeDuplicateUsingSet(array) + "\n" + "Without: " + "\n" + stored);

但也不是我一直想要的...。 它会不断删除所有重复的字符(例如,“ Samsung”将其显示为SAMUNG,如果要输入2个相同的元素(项目),我想在其中删除 请帮我。 预先感谢。

1 个答案:

答案 0 :(得分:0)

这是您想要的输出吗?

for line in get_tags('test.txt', tags):
    print(*(line[tag] for tag in tags))