Remove duplicates from integer array

How to remove duplicates from integer array using for each loop(Without using Distinct or LINQ)

2 Likes

Hi @Gihan_Chathuranga

Iterating through and checking each value in a for each statement works, but if you are looking at a large set of data it will take a long time. Instead, you could utilize LINQ to accomplish the task in a more efficient manner all in a single assign activity.

arr.GroupBy(Function(i) i ).Where(Function(g) g.Count() =1).Select(Function(s) s(0)).toArray()

Also another method

Assuming this is an array <T> (such as integer)

assign newArray = array.Distinct().ToArray()

1 Like

Hey @Gihan_Chathuranga

Assuming your variable as intArr

intArr.Distinct().ToArray()

Thanks
#nK

Hi nikhil.girish
Thank you for your answer.

Can you show me how to do it using for each loop. I want to use newArray as empty array. Then want to check one by one and assign it to the empty array using IF condition.
intArray = {1,2,3,3,2,4,5,5,1}
newArray = {}

Hi Nithinkrishna
Thank you for your answer.

Can you show me how to do it using for each loop. I want to use newArray as empty array. Then want to check one by one and assign it to the empty array using IF condition
intArray = {1,2,3,3,2,4,5,5,1}
newArray = {}

we would recommend to go for the oneliner with the distinct()

a for each approach would look like this:

distinctValues | List(of Int32) = new List(Of Int32)

For each | item in intArray - TypeArgument: Int32

  • If Activity: Not distinctValues .Contains(item)
    • then Branch: Add to collection Activity

On the end we can convert the list into an array by newArray = distinctValues .ToArray

1 Like

Hi @Gihan_Chathuranga

You can do the distinct functionality and then assign the same array to newly created array variable.

Thanks

1 Like

Hi @Gihan_Chathuranga

here you go

as PPR suggested even i do recommend you to go with one liner

Thanks