public class Allindices
{
public static void main(String[] args)
{
int[] arr={3,8,1,8,8,4};
int[] res=allIndices(arr,0,8,0);
display(res); //Why System.out.println(res); doesn’t work?
}
public static int[] allIndices(int[] arr,int i, int data, int count)
{
if(i==arr.length)
{
int[] base=new int[count];
return base;
}
int[] indices=null;
if(arr[i]==data)
{
indices=allIndices(arr,i+1,data,count+1);
}
else
{
indices=allIndices(arr,i+1,data,count);
}
if(arr[i]==data)
{
indices[count]=i;
}
return indices;
}
public static void display(int[] arr)
{
for( int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
}