Array.diff

kata programming

مساله:

کار شما در این کاتا پیاده سازی یک تابع تفاوت است که یک لیست را از لیست دیگر کم کرده و نتیجه را برمی گرداند.

باید مقادیر در لیست a را که در لیست b وجود دارد با رعایت ترتیب حذف کند.

Kata.arrayDiff(new int[] {1, 2}, new int[] {1}) => new int[] {2}

اگر مقداری در b وجود داشته باشد باید تمام مشترکات آن از لیست دیگر حذف شود.

Kata.arrayDiff(new int[] {1, 2, 2, 2, 3}, new int[] {2}) => new int[] {1, 3}

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b keeping their order.

Kata.arrayDiff(new int[] {1, 2}, new int[] {1}) => new int[] {2}

If a value is present in b, all of its occurrences must be removed from the other:

Kata.arrayDiff(new int[] {1, 2, 2, 2, 3}, new int[] {2}) => new int[] {1, 3}

راه حل ها (Solutions):

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Kata {
    public static int[] arrayDiff(int[] a, int[] b) {
        List<Integer> listA = Arrays.stream(a).boxed().collect(Collectors.toList());
        List<Integer> listB = Arrays.stream(b).boxed().collect(Collectors.toList());
        listA.removeAll(listB);
        return listA.stream().mapToInt(e -> e).toArray();
    }
}
import java.util.stream.IntStream;

public class Kata {

  public static int[] arrayDiff(int[] a, int[] b) {
    return IntStream.of(a).filter(x -> IntStream.of(b).noneMatch(y -> y == x)).toArray();
  }

}
import java.util.ArrayList;
import java.util.List;

public class Kata {

    public static int[] arrayDiff(int[] a, int[] b) {
        List<Integer> list = new ArrayList<>();
        loop:
        for (int num : a) {
            for (int i : b) if (num == i) continue loop;
            list.add(num);
        }

        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}
import java.util.Arrays;
import java.util.Objects;

public class Kata {

  public static int[] arrayDiff(int[] a, int[] b) {
    if(a == null || b == null) return a;
        
        return Arrays.stream(a)
                .filter(elemA -> Arrays.stream(b).noneMatch(elemB -> Objects.equals(elemA,elemB)))
                .toArray();
  }

}
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;

public class Kata {

  public static int[] arrayDiff(int[] a, int[] b) {
    
    for (int i = 0; i < b.length; i++)
      a = ArrayUtils.removeAllOccurences(a, b[i]);
    return a;
  }

}
import java.util.ArrayList;
public class Kata {

  public static int[] arrayDiff(int[] a, int[] b) {
    // Your code here
    ArrayList<Integer> output = new ArrayList<>();
     for (int i = 0; i < a.length; i++) {
        boolean flag = false;
        for (int j = 0; j < b.length; j++) {
            if (a[i] == b[j]) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            output.add(a[i]);
        }
    }
  int[] ret = new int[output.size()];
  for(int i = 0;i < ret.length;i++)
    ret[i] = output.get(i);
  

    
   

    
    
    
    return ret;
  }

}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Kata {

    public static int[] arrayDiff(int[] a, int[] b) {
        List<Integer> b1 = Arrays.stream(b).boxed().collect(Collectors.toList());
        return Arrays.stream(a).filter(i -> !b1.contains(i)).toArray();
    }
}

دیدگاهتان را بنویسید