Java > Array-2 > withoutTen (CodingBat Solution)

Problem:

Return a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces a the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and return the given array or make a new array.

withoutTen({1, 10, 10, 2}) → {1, 2, 0, 0}
withoutTen({10, 2, 10}) → {2, 0, 0}
withoutTen({1, 99, 10}) → {1, 99, 0}


Solution:

public int[] withoutTen(int[] nums) {
  int[] result = new int[nums.length];
  int j = 0;
  for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 10) {
    } else {
      result[j] = nums[i];
      j++;
    }
  }
  
  for(int i = j; i < nums.length; i++) {
    result[i] = 0;
  }
  return result;
}


30 comments :

  1. If you create new array, it will be filled zero's value by default. So, it is possible to solve this problem like this:
    public int[] withoutTen(int[] nums) {
    int[] tab = new int[nums.length];
    int counter = 0;
    for (int i = 0; i < nums.length; i++)
    {
    if (nums[i] != 10)
    tab[counter++] = nums[i];
    }
    return tab;
    }

    ReplyDelete
  2. without creating new array but 2 cycles:
    static int[] withoutTen(int[] nums) {
    int len = nums.length;

    for(int i=0; i<len; i++){
    if(nums[i]==10){
    for(int j=i; j<len-1; j++){
    nums[j] =nums[j+1];
    }
    nums[len-1]=0;
    i--;
    }
    }
    return nums;
    }

    ReplyDelete
    Replies
    1. this is THE BEST SOLUTION i need to know if we create new array it will cost memory but this one will take O(n2) it depends on ,THANKS

      Delete
  3. This is a different approach looks heavy but it isnt reallly
    public int[] withoutTen(int[] nums) {
    int ten = 0;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 10)
    for(int j = i; j < nums.length; j++)
    if(nums[j] != 10){
    int temp = nums[i];
    nums[i] = nums[j];
    nums[j] = temp;
    break;
    }
    }
    for(int i = 0; i < nums.length; i++)
    if(nums[i] == 10)
    nums[i] = 0;

    return nums;
    }

    ReplyDelete
  4. public int[] withoutTen(int[] nums) {
    int count=0;
    int index=0;
    int i=0;
    int[] arr=new int[nums.length];
    while(i<nums.length){
    if(nums[i]!=10){
    arr[index]=nums[i];
    index++;
    i++;
    }
    else {count++; i++;}
    }
    for (int j = 0; j < count; j++) {
    arr[index]=0;
    index++;
    }
    return arr;
    }

    ReplyDelete
  5. Using only one loop---
    public int[] withoutTen(int[] nums) {
    int result = 0;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] != 10){
    int temp = nums[i];
    nums[i] = 0;
    nums[result] = temp;
    result++;
    }else{
    nums[i] = 0;
    }
    }
    return nums;
    }

    ReplyDelete
    Replies
    1. Quite interesting u just swapped it cleverly 💖

      Delete
  6. public int[] withoutTen(int[] nums) {
    int count10 = 0;
    int not10= 0;
    int[] arr = new int[nums.length];
    for(int i = 0; i < nums.length; i++){
    if(nums[i] != 10){
    arr[not10] = nums[i];
    not10++;
    }else{
    arr[nums.length - count10 - 1] = 0;
    count10++;
    }
    }
    return arr;
    }

    ReplyDelete
  7. public static int[] withoutTen(int[] nums) {
    int index = 0;
    for (int i = 0; i < nums.length; i++) {

    if(nums[i] != 10) {
    int temp = nums[i];
    nums[i] = nums[index];
    nums[index] = temp;
    index++;

    }
    if(nums[i] == 10) {
    nums[i] = 0;
    }

    }


    return nums;
    }

    ReplyDelete
  8. I liked my solution.

    int [] d= new int [nums.length];
    int count=0;
    for(int i=0;i<nums.length;i++) {

    if(nums[i]!=10) {

    d[count++]=nums[i];
    }
    }

    return d;

    ReplyDelete
  9. public int[] withoutTen(int[] nums) {
    int[] r = new int[nums.length];
    int x = 0;

    for(int i = 0 ; i < nums.length ; i++) {
    if(nums[i] != 10) {
    r[x] = nums[i];
    x++;
    }
    }

    return r;
    }

    ReplyDelete
  10. public int[] withoutTen(int[] nums) {
    int currentShift =0;
    int medium =0;

    for(int i=0; i<nums.length; i++){

    if(nums[i] ==10 || nums[i] ==0 ){
    currentShift ++;
    nums[i]=0;
    }
    if(nums[i]!=10 && currentShift !=0 && nums[i]!=0){
    medium = nums[i];
    nums[i-currentShift] = medium;
    nums[i]=0;
    currentShift =1;

    }

    }

    return nums;
    }

    ReplyDelete
  11. One for loop, without creating new array

    public int[] withoutTen(int[] nums) {

    for(int i = 0, j = 0; j < nums.length; j++) {
    if(nums[j] == 10) {
    nums[j] = 0;
    }
    else {
    if(nums[i] == 0) {
    nums[i] = nums[j];
    nums[j] = 0;
    }
    i++;
    }
    }
    return nums;

    }

    ReplyDelete
  12. public int[] withoutTen(int[] nums) {
    int a[]=new int[nums.length];
    int k=0;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==10)
    continue;
    else
    a[k++]=nums[i];
    }
    return a;
    }

    ReplyDelete
  13. public int[] withoutTen(int[] nums) {
    //Two Pointer One Loop Solution//
    int i = 0;
    for (int j = 0; j < nums.length; j++)
    {
    if (nums[j] == 10)
    {
    while (i < nums.length && nums[i] == 10)
    {
    i++;
    }
    if (i < nums.length)
    {
    nums[j] = nums[i];
    nums[i] = 10;
    }
    else
    {
    nums[j] = 0;
    }
    }
    i++;
    }
    return nums;
    }

    ReplyDelete
  14. public int[] withoutTen(int[] nums) {
    int i = 0, j = 0;
    while (j < nums.length) {
    if (nums[j] != 10) {
    nums[i++] = nums[j];
    }
    j++;
    }
    while (i < nums.length) {
    nums[i++] = 0;
    }
    return nums;
    }

    ReplyDelete
  15. public class WithoutTen {
    public int[] withoutTen(int[] nums) {
    int[] tab = new int[nums.length];
    int counter = 0;
    for (int num : nums) {
    if (num != 10) {
    tab[counter++] = num;
    }
    }
    return tab;
    }
    }

    ReplyDelete
  16. public int[] withoutTen(int[] nums) {
    int[] myArray = new int[nums.length];
    int i =0;
    int j=0;
    while(i<nums.length) {
    if(nums[i] != 10) {
    myArray[j]=nums[i];
    i++;
    j++;

    } else{
    i++;
    }
    }
    return myArray;
    }

    ReplyDelete
  17. public int[] withoutTen(int[] nums) {
    int[] result = new int[nums.length];
    int j = 0;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] != 10) {
    //روح حطها في الاراي الجديده من اولها
    result[j] = nums[i];
    j++;
    }
    }

    /*for(int i = j; i < result.length; i++) { //املا الباقي ب اصفار
    result[i] = 0;
    }*/
    return result;
    }

    ReplyDelete
  18. /*public int[] withoutTen(int[] nums) {

    int c= nums.length-1;

    for( int i=0 ; i<nums.length ; i++){
    if(nums[i]==10){
    for(int j=i+1 ; j<nums.length ; j++){
    nums[j-1]=nums[j];
    }
    nums[c]=0; c--;

    }

    }
    return nums;
    }

    */

    ReplyDelete
    Replies
    1. for(int j=i+1 ; j<nums.length ; j++){
      nums[j-1]=nums[j];
      }
      بتعملها شيفت
      nums[c]=0; c--;
      وتصفر الاخيره

      Delete
  19. public int[] withoutTen(int[] nums) {
    int[] arr=new int[nums.length];
    int j=0;
    for(int i=0;i<nums.length;i++)
    if(nums[i]!=10){
    arr[j]=nums[i];
    j++;
    }
    for(int i = j; i < nums.length; i++) {
    arr[i] = 0;
    }
    return arr;
    }

    ReplyDelete
  20. public int[] withoutTen(int[] nums) {
    int array[] = new int[nums.length];
    int index=0, index1=1;
    for(int i=0; i<nums.length; i++){
    if(nums[i]!=10) {
    array[index]=nums[i];
    index++;
    } else {
    array[array.length-index1]=0;
    index1++;
    }
    }
    return array;
    }

    ReplyDelete
  21. without new array in python
    def withoutTen(n):
    c = 0
    for i in range(len(n)):
    if n[i] != 10:
    n.insert(c,n.pop(n.index(n[i])))
    c += 1
    else:
    n[i] = 0
    return n

    ReplyDelete
  22. The second for loop at the bottom is completely unnecessary, as an array of ints in Java automatically populates empty spaces with the default value of 0.

    ReplyDelete
  23. public int[] withoutTen(int[] nums) {
    int j = 0;
    int[] t = new int[nums.length];

    for (int i = 0; i < nums.length; i++) {
    if (nums[i] != 10) {
    t[j] = nums[i];
    j++;
    }
    }
    return t;
    }

    ReplyDelete
  24. With Java Stream

    public int[] withoutTen(int[] nums) {
    return java.util.stream.IntStream.of(nums).boxed()
    .map(i -> i == 10 ? 0 : i)
    .sorted(Comparator.comparing(n -> n == 0))
    .mapToInt(i->i).toArray();
    }

    ReplyDelete
  25. public int[] withoutTen(int[] nums) {
    int arr[]=new int[nums.length];
    int c=0;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==10){
    nums[i]=0;
    }
    if(nums[i]!=10&&nums[i]!=0){
    arr[c]=nums[i];
    c++;
    }

    }
    return arr;
    }

    ReplyDelete
  26. public int[] withoutTen(int[] nums) {
    int count = 0;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 10) {
    nums[i] = 0;
    }
    else {
    count++;
    }
    }
    boolean sorted = false;
    while (!sorted(nums,count)) {
    for (int i = nums.length - 1; i > 0; i--) {
    if (nums[i] != 0 && nums[i-1] == 0) {
    nums[i-1] = nums[i];
    nums[i] = 0;
    }
    }
    }
    return nums;
    }
    public boolean sorted(int[] nums, int count) {
    for (int i = 0; i < count; i++) {
    if (nums[i] == 0) {
    return false;
    }
    }
    return true;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact