How to delete duplicaties from a String

So I have a string (or text): Mark Jason Mark Jason Jason Mark

Or Amy John John Amy

and I would like to modify it so that all the duplicates are deleted

The first solution would be: Mark Jason
The second: Amy John

How do I do that?

try this @anon40731888

  1. Create a new Sequence
  2. Assign inputText = “Mark Jason Mark Jason Jason Mark” (or any other input string)
  3. Assign resultText = “”
  4. Create a Dictionary variable uniqueWordsDict = new Dictionary(Of String, Boolean)
  5. Assign wordsArray = inputText.Split({" "}, StringSplitOptions.RemoveEmptyEntries)
  6. For Each word In wordsArray
    • If Not uniqueWordsDict.ContainsKey(word), Then
      • Assign uniqueWordsDict(word) = True
    • Else
      • Assign uniqueWordsDict(word) = False
    • End If
  7. End For Each
  8. Assign resultText = String.Join(" ", uniqueWordsDict.Keys.Where(Function(key) uniqueWordsDict(key) = True))

Hi @anon40731888

inputString = “Mark Jason Mark Jason Jason Mark”

words = inputString.Split(’ ')

uniqueWords = words.Distinct()

outputString = string.Join(" ", uniqueWords)

Thank you but how do I do that in StudioX? where should I add the code?

Try adding it In assign activity

Hi @anon40731888
wordsArray = inputString.Split({" “}, StringSplitOptions.RemoveEmptyEntries)
distinctArray = wordsArray.Distinct().ToArray()
outputString = String.Join(” ", distinctArray)

Hope it helps!!

@anon40731888
Use set variable activity

BlankProcess.zip (2.6 KB)

Refer the above workflow @anon40731888

I have used
String.Join(" ", inputText.Split().Distinct())

Refer this

Hi @anon40731888

Use the below expression

String.Join(" ", strInput.Split(" "c).Distinct())

use the set variable activity to do this one in studio x

Hop it helps!!

@anon40731888

To delete duplicates from an array of strings in UiPath StudioX, you can follow these steps:

  1. Open UiPath StudioX and create a new project or open an existing one.

  2. Drag and drop a “Set Variable” action onto the canvas.

  3. In the “Variable” field, enter a name for the variable that will hold the result (e.g., resultArray).

  4. In the “Value” field, click on the “fx” button to open the Expression Editor.

  5. In the Expression Editor, enter the following expression to remove duplicates from the array:

    inputArray.Distinct().ToArray()
    

    Replace inputArray with the actual value or variable that contains your array of strings.

    The Distinct() function removes duplicates from the array, and ToArray() converts the distinct values back into an array.

  6. Click “OK” to close the Expression Editor.

  7. If you want to store the result back into the original array variable, follow these steps:

    • Drag and drop another “Set Variable” action onto the canvas.
    • In the “Variable” field, select or enter the name of your original array variable.
    • In the “Value” field, enter the name of the variable that holds the result (resultArray in this example).
  8. Continue with the rest of your workflow or add any additional actions as needed.

Here’s an example of the steps described above in UiPath StudioX:

In this example, the input array is {"Apple", "Banana", "Apple", "Orange", "Banana"} and the output array will be {"Apple", "Banana", "Orange"} with the duplicates removed.

Hope it works!!

strInput is name of my variable with names?

@anon40731888 absolutely correct it was a variable which the stores the text data.

let me try this now :)))))))

Okay @anon40731888

If you got this. Do mark it as solution which help to others.

BlankProcess.zip (2.6 KB)

Refer the above workflow @anon40731888

I have used
String.Join(" ", inputText.Split().Distinct())

Refer this i have sent the xaml to get clear understanding

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.