Solution to return array if it is repeated more than once

Hi,

i have an array.want to return a output array where if the a string has occured more than once.can someone help with a linq query.

Input array - {“aa”,“aa”,“bb”,“cc”,“dd”,“bb”,“cc”}

Output Array - {“aa”,“bb”,“cc”}

myArray.Distinct

This returns an array that has duplicates removed.

However, if you want to exclude those that don’t appear more than once then you’ll probably have to do a For Each on your Array, and then within each loop count how many times the current value exists in the array. If it’s more than one, add it to a secondary array.

Hi @JITU99

Use the below Linq querey.
string inputArray = { “aa”, “aa”, “bb”, “cc”, “dd”, “bb”, “cc” };

var outputArray = inputArray.GroupBy(s => s).Where(g => g.Count() > 1).Select(g => g.Key).ToArray()

// outputArray will contain [“aa”, “bb”, “cc”]

Hope it helps!!

@JITU99
Create Another Array → OutputArray
Use assign Activity;

Left Side → OutputArray
Right Side → InputArray.Where(Function(x) InputArray.Count(function(y) y=x)=1).ToArray

Enjoy. Please mark it as solution if you get help from this to resolve your problem.

Hi @raja.arslankhan Can i know what arrstring is for?

@JITU99 Sorry It was InputArray. I updated.

InputArray.Where(Function(x) InputArray.Count(function(y) y=x)>1).ToArray

1 Like

Thanks it worked.

But can you update the codition to >1 instead of =1

that fits my requirement and also have to apply distinct feature to filter out the repeated items in array

@JITU99
Yes. You can modify accordingly. Please mark it as a solution so others can help from this. Thanks

Done Thanks for quick help.

1 Like

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