• Latest Code...

    Count Occurrences in List item and String in KOTLIN

     



    Here is some way to find out how to count list item from the list, only first char in list and many more;

    1. Count first character with start particular char on list 

    fun countOccurrencesOnlyFirstCharUsingCount() {
    val list = listOf("one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine", "ten")
    val char = 't'
    val count = list.count { it.startsWith(char) }
    println("$char => $count")
    }

    output:

    /*
    t => 3
    */

    2. Count occurrence first all characters on list

    fun countOccurrencesFirstCharUsingGroupingBy() {
    val list = listOf("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
    val frequenciesByFirstChar = list.groupingBy { it.first() }.eachCount()
    println(frequenciesByFirstChar) // {o=1, t=3, f=2, s=2, e=1, n=1}
    }

    output:

    /*
    {o=1, t=3, f=2, s=2, e=1, n=1}
    */


    3. Count occurrence of word in list by using groupingBy and eachCount

    fun countOccurrencesUsingGroupingBy() {
    val list = listOf("usa", "china","japan","usa", "canada","usa","canada", "japan", "india","japan")
    println(list.groupingBy { it }.eachCount())

    }

    output:

    /*
    {usa=3, china=1, japan=3, canada=2, india=1}
    */

    4. Count occurrence of word in list by using  MutableMap and for loop

    fun countOccurrencesUsingMutableMap() {
    val list = listOf("usa", "china","japan","usa", "canada","usa","canada", "japan", "india","japan")
    val countMap : MutableMap<String, Int> = HashMap()
    for (item in list){
    var count = countMap[item]
    if (count == null) count = 0
    countMap[item] = count + 1
    }
    println(countMap)
    }

    output:

    /*
    {usa=3, china=1, canada=2, japan=3, india=1}
    */

    5. Count occurrence of word in list by using distinctCollections and, frequency

    fun countOccurrencesUsingCollection() {
    val list:List<String> = listOf("usa", "china","japan","usa", "canada","usa","canada", "japan", "india","japan")
    for(i in list.distinct()){
    println( "$i => ${Collections.frequency( list, i)}")
    }
    }

    output:

    /*
    usa => 3
    china => 1
    japan => 3
    canada => 2
    india => 1
    */

    6. Count occurrence of word in list without (expect) banned word or list   (.filterNotgroupingBy ,  eachCount)

    with help fo HashSet.

    fun countOccurrencesUsingGroupingByBannedItem() {
    val list = listOf("usa", "china","japan","usa", "canada","usa","canada", "japan", "india","japan")

    val banned = setOf("china","india")
    var bannedSet = banned.toHashSet()

    val wordCount = list.filterNot { it in bannedSet }.groupingBy { it } .eachCount()
    println(wordCount)
    }

    output:

    /*
    {usa=3, japan=3, canada=2}
    */


    HAPPY CODING :) 

    No comments:

    Post a Comment

    Circle

    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace