Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. package com.eip.chapter6;
  2.  
  3. import java.util.Arrays;
  4.  
  5. /**
  6. * Implement a function which takes as input an array and a key and updates the
  7. * array so that all occurrences of the key have been removed and the remaining
  8. * elements have been shifted left to fill the emptied indices. Return the number
  9. * of remaining key
  10. */
  11. public class RemoveKey {
  12. public int removeKey(int[] A, int key ){
  13. System.out.println("Entering RemoveKey.removeKey " + Arrays.toString(A));
  14. int copyCounter = 0;
  15. for(int i = 0; i < A.length;i++){
  16. if(A[i] != key)
  17. A[copyCounter++] = A[i];
  18. }
  19. System.out.println("Exiting RemoveKey.removeKey " + Arrays.toString(A));
  20.  
  21. return copyCounter;
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement