In this article we will create of array using kotlin language. All these program are successfully compiled and execute in online compiler. If you don’t familiar with array you can read here.

1:- Write a program to reverse of array in kotlin

fun main()
{
  val array= arrayOf(1,2,3,4,6,5,7) 
  var array2=arrayOfNulls<Int>(array.size)
   for(i in array.size-1 downTo 0)
    {
        array2[(array.size-1)-i]=array[i]
           
    }
     
    for(item in array2) 
       {
        print(" $item")
       }
   
}

Output

 7 5 6 4 3 2 1

2:- Write a program to remove duplicate element from array in kotlin

fun main()
{
   val intArray = arrayOf(6,3,5,3,2,5,1,2,2,3,4,6,6,6,5,5)
       val removeDuplicates: MutableSet<Int> = mutableSetOf()
    
    var temp:Int=0
    for (i in 0..(intArray.size-1)) {
		if(!removeDuplicates.contains(intArray[i])){
              removeDuplicates.add(intArray[i])
        }
    }

       println(removeDuplicates) 

}

Output

[6, 3, 5, 2, 1, 4]

3:- Write a program to sort element of array in kotlin

fun main()
{
   val intArray = arrayOf(-1, 2, -3, 4, 5, 6, -7, 8, 9,0,1,0)
        
    var temp:Int

    for (i in 0..intArray.size-1) {

        for (j in i + 1 until intArray.size) {
            if (intArray[i] > intArray[j]) {
                temp = intArray[i]
                intArray[i] = intArray[j]
                intArray[j] = temp
            }
        }
    }
    
    for(i in intArray)
    {
        print(" $i")
    }
}

Output

 -7 -3 -1 0 0 1 2 4 5 6 8 9

4:- Write a program to find duplicate element in array in kotlin

fun main()
{
   val intArray = arrayOf(6,3,5,3,2,5,1,2,2,3,4,6,6,6,5,5)
   var list = ArrayList<Int>()
   
  for(i in intArray)
    {
        var  counter=0

        for(j in intArray)
        {

            if(i==j)
            {
                counter++
            }
        }
        if(!list.contains(i) && counter>1)
        {   
            list.add(i)
            println("$i occurred at $counter times")

        }
    }    
}

Output

6 occurred at 4 times
3 occurred at 3 times
5 occurred at 4 times
2 occurred at 3 times

5:- Write a program to find common between two array in kotlin

fun main()
{
   val intArray = arrayOf(6,5,3,9,2,5,1,2,4,5)
   val intArray2 = arrayOf(6,8,9,10,5,2,1,4,5)
   var list=ArrayList<Int>()
    for (i in 0..intArray.size-1) {

        for (j in 0.. intArray2.size-1) {
            if (intArray[i] == intArray2[j]) 
            {
                if(!list.contains(intArray2[j]))
                {
                    list.add(intArray2[j])

                }
            }
   
        }

    }
  
    print("common element is $list")

}

Output

common element is [6, 5, 9, 2, 1, 4]

6:- Write a program of merging of two array in kotlin

fun main()
{
 val intArray = arrayOf(1,2,3,4,5)
 val intArray2 = arrayOf(6,7,8,9,10)
 var intArray3 = arrayOfNulls<Int>.(intArray.size+intArray2.size)
   
    for (i in 0..intArray.size-1) 
    {
   
 intArray3[i]=intArray[i]
    }
        for (j in 0.. intArray2.size-1) 
      {
     
       intArray3[intArray.size+j]=intArray2[j]
    
    }
    for (k in intArray3 ){
        
    print(" $k")
 
    }
}

Output

 1 2 3 4 5 6 7 8 9 10

7:- Write a program to find even number in array in kotlin

fun main()
{
   val intArray = arrayOf(1,2,3,4,5,6,7,8,9,10)
   val list=ArrayList<Int>()
    for (i in 0..intArray.size-1) {
   
        if(intArray[i]%2==0)
        {
            list.add(intArray[i])
        }
    }
              
    print("$list")

}

Output

[2, 4, 6, 8, 10]

8:- Write a program to cyclically rotate an array by one in kotlin

fun main()
{
   val intArray = arrayOf(1,2,3,4,5)
   
   var last=intArray[intArray.size-1]
   for (i in intArray.size-1 downTo 1) {
     intArray[i]=intArray[i-1]
    }
    intArray[0]=last

   for (i in intArray) {
     print(" $i")
    }

}

Output

 5 1 2 3 4

9:- Write a program to Largest Sum Contiguous Subarray (Kadane’s Algorithm) in kotlin

fun main()
{
   val intArray = arrayOf(-1,-2,-3,-4,1,3,4)
   var max_so_far=Integer.MIN_VALUE
   var max_end=0 
    for (i in 0..intArray.size-1) {
          
          max_end = max_end + intArray[i];
            if (max_so_far < max_end)
                max_so_far = max_end;
            if (max_end < 0)
                max_end = 0;
    }

         print("$max_so_far")

}

Output

8

10:- Write a program to alternate swap in kotlin

fun main()
{
   val intArray = arrayOf(1,2,3,4,5,6)
    for (i in 0..intArray.size-1 step 2) {
          
      if((i+1)<intArray.size)
        {
            var temp=0
           temp=intArray[i]
            intArray[i]=intArray[i+1]
            intArray[i+1]=temp
        }
    }

   for (i in intArray) {
         
       print(" $i")
    }

}

Output

 2 1 4 3 6 5

11:- Write a program to check first duplicate in kotlin

fun main()
{
   val intArray = arrayOf(6,3,5,3,2,5,1,2,4,5,5) 
    var temp:Int=0
    for (i in intArray.indices) {
        for (j in 0 until intArray.size) {
            if (intArray[i] == intArray[j]&&(i!=j)) {
                    println(" First duplicate: "+intArray[i])
                    temp++
                    break
            }
            if(temp>0)
            {
                break
            }
        }
    }  
}

Output

 First duplicate: 3

12:- Write a program to missing number in Array one in kotlin

fun main()
{
  val array= arrayOf(1,2,3,4,6,5,7)
  var sum=0
  var expected=array.size+1
    var tottal_sum= (expected*(expected+1))/2
     for(i in array.indices)
    {
        sum=sum+array[i]
    }

    print("missing is ${tottal_sum-sum}")
}

Output

missing is 8

13:- Write a program to remove element in Array in kotlin

fun main()
{
  val array= arrayOf(1,2,3,4,6,5,7) //6
  var array2=arrayOfNulls<Int>(array.size-1) //5
  var sum=3
  var k=0

   for(i in array.indices)
    {
       
        if(sum!=array[i])
        {
            array2[k++]=array[I]

        } 
 }
     for(item in array2) 
       {
        print(" $item")
       } 
}

Output

 1 2 4 6 5 7

14:- Write a program to find occurrence of element in array using hash map in kotlin

fun main()
{
   val intArray = arrayOf("java","kotlin","kotlin","c","java","c","java","go")
   var hashmap : HashMap<String, Int> = HashMap<String, Int> ()
  	for(i in intArray){
    if(hashmap.containsKey(i)){
        hashmap[i] = hashmap[i]!! + 1
    }
    else{
        hashmap[i] = 1
    }
    }
    
    print(hashmap)
    
}

Output

{java=3, c=2, kotlin=2, go=1}

15:- Write a program to find unique element in Array in kotlin

fun main()
{
   val intArray = arrayOf(1,2,3,4,5,5,4,3,2,6,11)
  	for(i in 0..intArray.size-1){
           var counter=0

         for(j in 0..intArray.size-1){
          if(intArray[i]==intArray[j])
           {
  	  counter++
	 }        
 
   }  
    if (counter==1)
    (    
        println(intArray[i])

    )
    }
    
}

Output

1
6
11

You may also like...

0 Comments

No Comment.