fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int find(char[] array, char[] subs)
  11. {
  12. int found = 0;
  13. for (int x = 0; x < subs.length; x++)
  14. {
  15. for (int y = 0; y < array.length; y++)
  16. {
  17. if (subs[x] == array[y])
  18. {
  19. found++;
  20.  
  21. // Y is the index of the element found in the original array
  22. // we must erase this element so it's not found again.
  23. char[] smaller_array = new char[array.length-1];
  24. for (int i = 0; i < array.length; i++)
  25. {
  26. if (i < y)
  27. smaller_array[i] = array[i];
  28.  
  29. if (i == y)
  30. continue;
  31.  
  32. if (i > y)
  33. smaller_array[i-1] = array[i];
  34. }
  35.  
  36. array = smaller_array;
  37. break;
  38. }
  39. }
  40. }
  41.  
  42.  
  43. return found;
  44. }
  45.  
  46. public static void main (String[] args) throws java.lang.Exception
  47. {
  48. char[] array1 = { 'a','b','c','d','e','f','x','r' };
  49. char[] sub = { 'd','e','f' };
  50. System.out.println("Number of matches with array #1: " + find(array1, sub));
  51.  
  52. char[] array2 = { 'g','e','h','i','d','k','x','f' };
  53. System.out.println("Number of matches with array #2: " + find(array2, sub));
  54.  
  55. char[] array3 = { 'd' };
  56. System.out.println("Number of matches with array #3: " + find(array3, sub));
  57.  
  58. char[] array4 = { 'd','d','d' };
  59. System.out.println("Number of matches with array #4: " + find(array4, sub));
  60.  
  61. char[] array5 = { 'd','e','f' };
  62. System.out.println("Number of matches with array #5: " + find(array5, sub));
  63.  
  64. char[] array6 = { 'f','d','e' };
  65. System.out.println("Number of matches with array #6: " + find(array6, sub));
  66.  
  67. char[] array7 = { 'a','b','c','g','h','i','j','k' };
  68. System.out.println("Number of matches with array #7: " + find(array7, sub));
  69. }
  70. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Number of matches with array #1: 3
Number of matches with array #2: 3
Number of matches with array #3: 1
Number of matches with array #4: 1
Number of matches with array #5: 3
Number of matches with array #6: 3
Number of matches with array #7: 0