0

I'm trying to find duplicates in an array and put only the duplicates in an array. The code im using is the following

    Dim duplicates As List(Of String) =
               WithDuplicates.GroupBy(Function(n) n) _
               .Where(Function(g) g.Count() > 1) _
               .Select(Function(g) g.First) _
               .ToList()
    Dim DuplicatesOnly As String() = duplicates.ToArray

WithDuplicates is the Array that contains the duplicates. Before, I used a richtextbox instead of a array but I had to switch to an array because of threading. I don't know why my code isnt working anymore. Any ideas how I could get this working?

Edit: The code above does indeed work, another part of my code caused the error.

7
  • 1
    What is not working? Note that it's a case-sensitive comparison; what if you add StringComparer.InvariantCultureIgnoreCase (or StringComparer.CurrentCultureIgnoreCase) to the grouping? Commented Jun 10, 2022 at 14:31
  • @Jimi Unfortunately I dont even know what isn't working. Where would I put your code? Commented Jun 10, 2022 at 14:35
  • 2
    How could you possibly not know what is wrong with that code? You said my code isn't working anymore, so how did it work before? Is the content of DuplicatesOnly that is different from what you expected? Something else? -- What has Threading to do with this? Commented Jun 10, 2022 at 14:38
  • @jimi haha right, my thought exactly. Moving from a control to a List might have something to do with a UI cross thread issue Commented Jun 10, 2022 at 14:40
  • 1
    Well, then DuplicatesOnly is empty because no duplicates were found or WithDuplicates is empty -- When you post your code, you always need to give it context. You also need to debug your code: put a breakpoint in the first line, inspect the variables / values and step in one line at the time. Get rid of any MessageBox, print to the Output Window, when needed, instead (Console.WriteLine() or Debug.WriteLine()) -- What is the content of WithDuplicates and where does it come from? How is threading involved? Update your questions with this information. Commented Jun 10, 2022 at 15:45

3 Answers 3

2

This might help you:

Dim duplicates As List(Of String) = WithDuplicates.Distinct.Where(Function(el) Array.IndexOf(WithDuplicates, el) <> Array.LastIndexOf(WithDuplicates, el)).ToList
Sign up to request clarification or add additional context in comments.

Comments

1

You almost had it. You just need to select the key instead of the first.

Dim withDuplicates As New List(Of String) From {"a", "b", "c", "a", "c"}

Dim duplicates As List(Of String) =
    withDuplicates.GroupBy(Function(n) n).
    Where(Function(g) g.Count() > 1).
    Select(Function(g) g.Key).ToList()
Dim DuplicatesOnly As String() = duplicates.ToArray

Console.WriteLine(String.Join(", ", DuplicatesOnly))

a, c

1 Comment

@Jimi yeah I guess, haven't had my coffee yet... Maybe I should have waited until I heard "what" isn't working
1

Your code works if there are duplicates. Jimi's first comment may be pertinent, but your question lacks that detail.

    Dim WithDuplicates As New List(Of String) From {"aa", "b", "c", "aa", "c", "AA", "c"}
    ' Dim WithDuplicates As New List(Of String) From {"aa", "b", "c"} ', "aa", "c", "AA", "c"}

    Dim duplicates() As String
    duplicates = WithDuplicates.GroupBy(Function(n) n).
                    Where(Function(g) g.Count() > 1).
                    Select(Function(g) g.First).ToArray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.