Remove Random item from List

Write a Program to remove the random items from the List.

Example:

Input:[Aman, Rahul, sweta, sonam, Deven]
Output:[Aman, sweta, sonam, Deven]

Approach:

Java


import java.util.*;
public class Main
{
    public static void main(String[] args) {

        List<Stringlist = new ArrayList<>();

        list.add("Aman");
        list.add("Rahul");
        list.add("sweta");
        list.add("sonam");
        list.add("Deven");

     System.out.println("list before removal of random name:"+list);

        int lenght=list.size();

        Random random = new Random();

        int indexrandom.nextInt(lenght - 0) + 0;

        String nameTobeRemove=list.get(index);

        System.out.println("Name to be remove is:"
+nameTobeRemove);

        list.remove(nameTobeRemove);

      System.out.println("list after removal of random name:
"+list);

        
    }
}

No comments:

Post a Comment