import java.util.*;
public class rearrange_palindrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String st=sc.nextLine();
ArrayList<Character> ar = new ArrayList<Character>();
for (int i=0;i<st.length();i++){
if (ar.contains(st.charAt(i)))
ar.remove((Character)st.charAt(i));//why did they use type casting?
else
ar.add(st.charAt(i));
}
if (st.length()%2==0 && ar.isEmpty()==true || st.length()%2==1 && ar.size()==1)
System.out.println("Palindrome");
else
System.out.println("Not a palindrome");
}
}
please tell, me the reason why did they use type casting, I've commented that line.
Removing both chars and ints can lead to logic errors when using the
remove(T e)because of the overloaded methodremove(int index)of the ArrayList class. Casting to their wrapper class avoids this.