- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 08:39 PM
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 10:29 PM
Hello @xiyan_li ,
Please try below script i have tried in background script and its working
var str1 = 'abc,str_abc,try,abc,str_abc';
var uniqueValues = [];
var check = {};
if (str1) {
var items = str1.split(','); // split in array
for (var i = 0; i < items.length; i++) {
var item = items[i].trim(); // Trim whitespace
if (!check[item]) {
check[item] = true; // Mark this item as check
uniqueValues.push(item); // Add it to the unique values
}
}
}
// Join the unique values back into a string
var str2 = uniqueValues.join(',');
gs.print('Original string: ' + str1);
gs.print('String without duplicates: ' + str2);
O/P
*** Script: Original string: abc,str_abc,try,abc,str_abc
*** Script: String without duplicates: abc,str_abc,try
Kindly mark correct and helpful, if applicable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 09:13 PM - edited 09-17-2024 09:13 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 10:11 PM
Hello,The following error has occurred:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 10:29 PM
Hello @xiyan_li ,
Please try below script i have tried in background script and its working
var str1 = 'abc,str_abc,try,abc,str_abc';
var uniqueValues = [];
var check = {};
if (str1) {
var items = str1.split(','); // split in array
for (var i = 0; i < items.length; i++) {
var item = items[i].trim(); // Trim whitespace
if (!check[item]) {
check[item] = true; // Mark this item as check
uniqueValues.push(item); // Add it to the unique values
}
}
}
// Join the unique values back into a string
var str2 = uniqueValues.join(',');
gs.print('Original string: ' + str1);
gs.print('String without duplicates: ' + str2);
O/P
*** Script: Original string: abc,str_abc,try,abc,str_abc
*** Script: String without duplicates: abc,str_abc,try
Kindly mark correct and helpful, if applicable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 10:34 PM - edited 09-17-2024 10:36 PM
1st approach:
var str2 = [];
var str1=['a','b','a','c'];
for (var i = 0; i < str1.length; i++) {
if (str2.indexOf(str1[i]) === -1) {
str2.push(str1[i]);
}
}
For removing duplicate values in array, Use the built-in ArrayUtil Script Include:
2nd approach : var noDuplicates = new ArrayUtil().unique(str1);