scriptlet

How to search for a duplicate in a given string using scriptlet?


How can I search for a duplicate in a given string using a scriptlet?

ScripletInput= a,b,c,a

Here the letter 'a' is repeating. If it is repeating more than once, then it should exit, else it can go ahead.


Solution

  • Please see Remove occurrences of duplicate words in a string

    The code below will remove duplicates in a string.

    <script type="text/javascript">
        str=prompt("Enter String::","");
        arr=new Array();
        arr=str.split(",");
        unique=new Array();
        for(i=0;i<arr.length;i++)
        {
            if((i==arr.indexOf(arr[i]))||(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])))
                unique.push(arr[i]);   
        }
        unique.join(",");
        alert(unique);
    </script>