18

I have a string in javascript where there are a lot of duplicates. For example I have:

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"

What can I do to delete duplicates and to get for example x="Int32,Double"?

harry-potter
  • 1,981
  • 5
  • 29
  • 55
  • 6
    Have you tried *anything*? – meskobalazs Feb 24 '16 at 18:08
  • Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Liam Nov 04 '21 at 09:09

12 Answers12

55

With Set and Array.from this is pretty easy:

Array.from(new Set(x.split(','))).toString()

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"
x = Array.from(new Set(x.split(','))).toString();
document.write(x);
MinusFour
  • 13,913
  • 3
  • 30
  • 39
4

If you have to support current browsers, you can split the array and then filter it

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double";
    
var arr = x.split(',');
    
x = arr.filter(function(value, index, self) { 
    return self.indexOf(value) === index;
}).join(',');

document.body.innerHTML = x;
adeneo
  • 312,895
  • 29
  • 395
  • 388
3

Use new js syntax remove Dupicate from a string.

String.prototype.removeDuplicate = Function() {
  const set = new Set(this.split(','))
  return [...set].join(',')
}
x.removeDuplicate()
2
  function myFunction(str) {
    var result = "";
    var freq = {};
    for(i=0;i<str.length;i++){
       let char = str[i];
       if(freq[char]) {
        freq[char]++;      
       } else {
        freq[char] =1
        result = result+char;
       }
    }
    return result;
  }
2

That is a more readable and better parameterized solution:

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"
var removeDup = [...new Set(x.split(","))].join(",");

//result "Int32,Double"

Crepkey
  • 386
  • 1
  • 4
  • 8
2

Its simple just remove duplicates in string using new Set and join them.

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double";
console.log([...new Set(x)].join(""));
1

Check This out -

removeDuplicates() function takes a string as an argument and then the string split function which is an inbuilt function splits it into an array of single characters. Then the arr2 array which is empty at beginning, a forEach loop checks for every element in the arr2 - if the arr2 has the element it will not push the character in it, otherwise it will push. So the final array returned is with unique elements. Finally we join the array with the join() method to make it a string.

const removeDuplicates = (str) => {
const arr = str.split("");
const arr2 = [];

arr.forEach((el, i) => {
 if (!arr2.includes(el)) {
    arr2.push(el);
   }
 });
 return arr2.join("").replace(",", "").replace("", " ");
};

console.log(removeDuplicates( "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"));
Mohammad Basit
  • 971
  • 6
  • 18
0
function removeDuplicate(x)
{
    var a = x.split(',');
    var x2 = [];
    for (var i in a)
        if(x2.indexOf(a[i]) == -1) x2.push(a[i])
    return  x2.join(',');
}
thanhpk
  • 3,900
  • 4
  • 29
  • 36
0
function removeDups(s) {
let charArray = s.split("");
  for (let i = 0; i < charArray.length; i++) {
    for (let j = i + 1; j < charArray.length; j++)
      if (charArray[i] == charArray[j]) {
        charArray.splice(j, 1);
        j--;
      }
  }
  return charArray.join("");
}
console.log(removeDups("Int32,Int32,Int32,InInt32,Int32,Double,Double,Double"));
Darvin Kumar
  • 123
  • 2
  • 8
0

You can use Set()

const result = Array.from(new Set(x)).join('')

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"
const result = Array.from(new Set(x)).join('')

console.log(result)
Kritish Bhattarai
  • 1,501
  • 15
  • 20
0

you can use the replaceAll function:

let str = "/Courses/"

let newStr = str.replaceAll('/', '')

console.log(newStr) // result -> Courses

-1

const str =  "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double";
const usingSpread = [...str]
const duplicatesRemove = [...new Set(usingSpread)]
const string = duplicatesRemove.join("")
console.log("After removing  duplicates: " + string)

STEPS

  1. convert string to character array using spread operator
  2. new Set will implicitly remove duplicate character
  3. convert character array to string using join("") method
  • Your code splits the input string into individual characters instead of commas. It works for the OP's example, but will fail for inputs like `"Int32,Float,Double"`. – Phil Kang May 26 '21 at 09:47