How to implement the function indexOfAny() in Java

1 Answer

0 votes
public class MyClass
{
    public static int indexOfAny(String string, char[] array) {
        int lowestIndex = -1;
         
        for (char ch : array) {
            int index = string.indexOf(ch);
            if (index > -1)  {
                if (lowestIndex == -1 || index < lowestIndex) {
                    lowestIndex = index;
                    if (index == 0) {
                        break;
                    }
                }
            }
        }
 
        return lowestIndex;
    }
     
    public static void main(String[] args)
    {
        String str = "java programming";
         
        int i = indexOfAny(str, new char[] {'x', 'm', 'y'});
         
        System.out.print(i);
    }
}
 
 
 
 
 
 
/*
run:
  
11
  
*/

 



answered Aug 30, 2023 by avibootz
edited Sep 3, 2023 by avibootz

Related questions

1 answer 191 views
1 answer 121 views
1 answer 165 views
1 answer 148 views
148 views asked Sep 29, 2023 by avibootz
1 answer 130 views
1 answer 130 views
1 answer 196 views
...