Erste Schritte Arrays vergleichen und die zahlen die nur einmal vorkommen ausgeben

dan1996

Aktives Mitglied
Hallo, ich wollte eine Methode schreiben die 2 Arrays vergleicht und die zahlen, die
insgesamt genau einmal vorkommen im int[] array returnen.
Ich habe was versucht und zwar erst mal die Arrays nach Größe sortiert und dann wollte ich die vergleichen, was aber nicht genau hinkommt. Hat jemand eine Idee ?
z.B:
int[] a = {2,7,4,-3};
int[] b = {3,1,2,3};

= {-3,1,4,7}
Java:
public static void main(String[] args){
       
        int[] a = {2,7,4,-3};
        int[] b = {3,1,2,3};

        for(int i = 0; i < once(a,b).length; i++){ // array ausgeben
            System.out.print(once(a,b)[i] + " ");
        }
        System.out.println();
       

    }
    public static int[] once(int[] a, int[] b){
        a = sortier(a);
        b = sortier(b);
        for(int i = 0; i < a.length; i++){ // vergleichen
            for(int j = 0; j < b.length-1 ; j++){
                if(a[i] == b[j]){
                    a[i] = b[j+1];


                }
            }
        }
        return a;
    }

    // nach größe sortieren
    public static int[] sortier(int[] x){
        int temp;
        for(int i = 1; i < x.length; i++){
            for(int j = 0; j < x.length - i; j++){
                if(x[j] > x[j+1]){
                    temp = x[j];
                    x[j] = x[j+1];
                    x[j+1] = temp;
                }
            }
        }
        return x;
    }

Code:
Ausgabe:
-3 3 4 7
 
Es macht doch überhaupt keinen Unterschied, ob du nun in einem, zwei oder tausend Arrays guckst, ob eine Zahl nur einmal vorkommt... es reicht doch, wenn du die Konkatenation beider Arrays sortierst und prüfst.
 
With the power of streams:
Java:
import static java.util.Arrays.*;
import static java.util.function.Function.*;
import static java.util.stream.Collectors.*;
import java.util.*;
import java.util.stream.*;
public class NumbersOnlyOnce {
  public static IntStream once(int[]... arrays) {
    return stream(arrays)
        .flatMapToInt(array -> IntStream.of(array))
        .boxed()
        .collect(groupingBy(identity(), counting()))
        .entrySet()
        .stream()
        .filter(e -> e.getValue() == 1L)
        .mapToInt(Map.Entry::getKey)
        .sorted();
  }
  public static void main(String[] args) {
    once(
        new int[]{2,7,4,-3}, 
        new int[]{3,1,2,3})
    .forEach(System.out::println);
  }
}
 
Vielen Dank für deine Antwort 🙂 ich übe grade für meine Programmierklausur und das war eine Aufgabe davon, nur leider darf man nichts importieren... werde deine Methode ausprobieren aber ich muss irgendwie noch auf eine Lösung kommen, wie man das ohne "import" macht
 
With the power of streams:
With the power of mihe7 😛
Java:
public class Once {
    private static int[] combine(int[] a, int[] b) {
        int[] result = new int[a.length + b.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = a[i];
        }
        for (int i = 0; i < b.length; i++) {
            result[a.length + i] = b[i];
        }
        return result;
    }

    private static int[] sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if (arr[j] < arr[i]) {
                    int tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
        return arr;
    }

    private static int[] distinct(int[] arr) {
        int[] tmp = new int[arr.length];
        if (arr.length == 0) {
            return tmp;
        }

        int last = 0;
        int count = 0;
       
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] != arr[last]) {
                if (i == last+1) {
                    tmp[count++] = arr[last];
                }
                last = i;
            }
        }
        if (last == arr.length - 1) {
            tmp[count++] = arr[last];
        }


        int[] result = new int[count];
        for (int i = 0; i < count; i++) {
            result[i] = tmp[i];
        }

        return result;  
    }

    public static void main(String[] args) {
        int[] a = {2,7,4,-3};
        int[] b = {3,1,2,3};        
        int[] c = distinct(sort(combine(a, b)));
        System.out.print("{");
        for (int i = 0; i < c.length; i++) {
            System.out.print(i == 0 ? "" : ", ");
            System.out.print(c[i]);
        }
        System.out.println("}");
    }
}
Verzichtet für die Ausgabe auf alles - vor allem auf Eleganz *hust*
 

Zurück
Oben