How to implement the function lastIndexOfAny() in Java

1 Answer

0 votes
public class MyClass
{
    public static int lastIndexOfAny(String string, char[] array) {
		int highestIndex = -1;
		
		for (char ch : array) {
			int index = string.lastIndexOf(ch);
			if (index > highestIndex) {
				highestIndex = index;

				if (index == string.length() - 1)
					break;
			}
		}

		return highestIndex;
	}
     
    public static void main(String[] args)
    {
        String str = "java programming";
         
        int i = lastIndexOfAny(str, new char[] {'x', 'a', 'y'});
         
        System.out.print(i);
    }
}
 
 
 
 
 
 
/*
run:
  
10
  
*/

 



answered Sep 3, 2023 by avibootz

Related questions

1 answer 132 views
1 answer 126 views
1 answer 131 views
1 answer 122 views
1 answer 124 views
1 answer 190 views
1 answer 181 views
...