Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Finding occurances of a value in the elements of a list
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Trying out few ways to find occurances of a value in the elements of a list List<int> myList = new List<int>(); myList = new List<int> ( new int[] {3, 3, 4, 3, 5, 3} ); int count = 0; foreach (int y in myList.FindAll(x => x==3)){ // returns the value of all hits Console.WriteLine(y); count ++; } Console.WriteLine(string.Format("{0} occurances of 3 in myList",count)); Console.WriteLine(string.Format("the first occurance of value 4 is in myList[{0}]\n", myList.FindIndex(x => x == 4))); // returns only the first encountered hit // method 2 count = 0; List<string> myStrList = new List<string> ( new string[] {"go", "go", "gone", "goose", "goon"} ); foreach (string y in myStrList.FindAll(x => x=="go")){ // returns the value of all hits (not the indices) Console.WriteLine(y); count++; } Console.WriteLine(string.Format("{0} occurances of \"go\" in myStrList\n",count)); } } }
run
|
edit
|
history
|
help
0
Testing for Integers and Floats in a String
Validate Norwegian Mobile Numbers
Using Events/Delegates to Subscribe To A Service - Emails
Factorial using Recursion
a
Hello world
438935
comb sort
Simple Regex Example
test1