how to Find and count duplicate numbers in a string array in vb.net? -


how count duplicate numbers exist in string or integer array in vb.net?

dim string = "3,2,3" 

from above "a" variable want count of "3" 2 (i mean 3 exist 2 times) , "2" "1". how make in vb.net?????

actually above string "a" sql database. dont know numbers there. that's why asking here.

you've got answers choose from, thought you'd interested in 1 liner solution.

module module1     sub main()         dim str() string = "1,2,1,2,3,1,0,1,4".split(","c)         str.distinct().tolist().foreach(sub(digit) console.writeline("{0} exists {1}", digit, str.count(function(s) s = digit)))         console.readline()     end sub end module 

explanation what's happening:

  • str.distinct() - returns ienumerable object of unique items in array
  • .tolist() - turns ienumerable object list<t>
  • .foreach() - iterates through list<t>
    • sub(digit) - defines action delegate perform on each element. each element named digit during each iteration.
    • you should know console.writeline() doing
    • str.count() - count each occurrence digit satisfies condition
      • function(s) s = digit - defines func delegate count each occurrence of digits in array. each element, in str(), during count iterations stored in variable s , if matches digit variable sub(digit) counted

results:

1 exists 4 2 exists 2 3 exists 1 0 exists 1 4 exists 1 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -