Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

if array has multiple duplicate value number them accordingly

var res = renameFiles(keys);
        function renameFiles(arr){
          var count = {};
          arr.forEach(function(x,i) {
            if ( arr.indexOf(x) !== i ) {
              var c = x in count ? count[x] = count[x] + 1 : count[x] = 1;
              var j = c + 1;
              var k = x + '(' + j + ')';

              while( arr.indexOf(k) !== -1 ) k = x + '(' + (++j) + ')';
              arr[i] = k;
            }
          });
          return arr;
        }
 
PREVIOUS NEXT
Tagged: #array #multiple #duplicate #number
ADD COMMENT
Topic
Name
4+5 =