Feld filtern (3) (Felder)
Füllen Sie ein Feld mit den Zahlen:
2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14.
Schreiben Sie nun ein Programm, das aus dem obigen Feld ein neues Feld erzeugt und das alle Duplikate eliminiert. Der Prototyp der Funktion soll wie folgt aussehen:
unikate(original: integer[]): integer[]
1 Kommentare
9 Lösung(en)
#Loesung mit statischen Array
def unikate(a):
Max = 99
b = [False]*Max
# Zahlen markieren
for z in a:
b[z]=True
# Auslesen
counter = 0
out = []
for k in b:
if k:
out.append(counter)
counter = counter + 1
return out
# Loesung mit assoziativen Arrays
def unikate2(a):
d = {}
for z in a:
d[z]=1
return d.keys()
# Loesung mit set()
def unikate3(a):
b=list(set(a))
return b
# Test
zahlen = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14]
print unikate(zahlen)
print unikate2(zahlen)
print unikate3(zahlen)
public class feldfilltern3 {
public static void main(String[] args) {
new feldfilltern3().Top();
}
private void Top() {
int[] zaehlen = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
int zaehler = 0;
int i;
int[] neuzahlen = new int[12];
i = duplikate(zaehlen, neuzahlen);
int[] neuzahlen2 = abfüllen(neuzahlen, i);
ausgabe(neuzahlen2);
}
private int[] abfüllen(int[] neuzahlen, int i) {
int[] neuzahlen2 = new int[i];
int i2 = 0;
for (int z : neuzahlen) {
if (i2 < i) {
neuzahlen2[i2] = z;
i2++;
}
}
return neuzahlen2;
}
private int duplikate(int[] zaehlen, int[] neuzahlen) {
int i = 0;
for (int z : zaehlen) {
if (kommtNichtVor(z, neuzahlen, i)) {
neuzahlen[i] = z;
i++;
}
}
return i;
}
private boolean kommtNichtVor(int z, int[] neuzahlen, int erstesfreiesfeld) {
if (erstesfreiesfeld == 0) {
return true;
}
for (int j = 0; j < erstesfreiesfeld; j++) {
if (neuzahlen[j] == z) {
return false;
}
}
return true;
}
private void ausgabe(int[] zaehlen) {
for (int z : zaehlen) {
System.out.println(z);
}
}
}
Lösung von: Lulzim Bilali (Keine)
package ch.santis.programmierenlernen.kapitel6;
import java.util.Arrays;
public class Aufgabe_6_5 {
public static void main(String[] args) {
new Aufgabe_6_5().top();
}
// Aufgabe: Alle duplikate eliminieren und in neuen Array schreiben lassen
void top() {
int [] array1 = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 17, 14, 17}; // Hat zwei extra Zahlen 17 um auch vierfache und mehr zu behandeln
Arrays.sort(array1);
// Array2 mit Index-Länge von Array1
int [] array2;
array2 = new int [array1.length];
// Keine Dublikate von Array1 in Array2 schreiben und Array3 Index-Länge bestimmen
int array3length = 1;
int index2 = 0;
for(int index = 0; index < array1.length-1; index++) {
if(array1[index] != array1[index+1]) {
array2[index2++] = array1[index];
array3length++;
}
}
array2[index2] = array1[array1.length-1];
// Array3 deklarieren und initialisieren (Alle Zahlen von Array2 mit der berechneter Index-Länge)
int array3[];
array3 = new int [array3length];
for(int index = 0; index < array3length; index++) {
array3[index] = array2[index];
}
// Printen von Array3
for(int indexwert : array3) {
System.out.print(indexwert + " ");
}
}
}
Lösung von: Jan Roth (Santis Training AG)
import java.util.Arrays;
public class Feldfiltern_03 {
public static void main(String[] args) {
new Feldfiltern_03().top();
}
void top() {
int array[] = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
System.out.println("Originales Array \n" + Arrays.toString(array) + "\n");
int ohneDuplikat[] = ohneDuplikate(array);
System.out.println("Array Ohne Duplikate \n" + Arrays.toString(ohneDuplikat));
}
boolean pruefen(int[] original, int zahl) {
boolean duplikat = false;
for (int i = 0; i < original.length; i++) {
if (original[i] == zahl) {
duplikat = true;
}
}
return duplikat;
}
int[] ohneDuplikate(int original[]) {
int tempArray[] = new int[original.length];
// Temporäres Array sortieren und duplikate entfernen
for (int i = 0; i < original.length; i++) {
if (!pruefen(tempArray, original[i])) {
tempArray[i] = original[i];
}
}
// Neue Array länge definieren
int neueLaenge = 0;
for (int i = 0; i < tempArray.length; i++) {
if (tempArray[i] > 0) {
neueLaenge++;
}
}
// Neues Array erstellen und füllen
int neuesArray[] = new int[neueLaenge];
int neuerIndex = 0;
for (int i = 0; i < tempArray.length; i++) {
if (tempArray[i] > 0) {
neuesArray[neuerIndex] = tempArray[i];
neuerIndex++;
}
}
return neuesArray;
}
}
Lösung von: Benjamin Hägi (Santis Training AG)
let theArr = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14],
uniques = theArr.slice() // dies fertig eine kopie eines arrays an
for (let i = 0; i < uniques.length; i++) {
let del = uniques.lastIndexOf(uniques[i]);
if (del != i) {
uniques.splice(del, 1);
uniques.splice(i, 1);
i--;
}
}
console.log(uniques);
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
var lstOld = new List<int> { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
var lstNew = lstOld.GroupBy(x => x).Select(x => x.Take(1)).SelectMany(x => x).ToList();
Console.WriteLine(string.Join(", ", lstNew));
Lösung von: Jens Kelm (@JKooP)
// NET 6.x | C# 10.x | VS-2022
var lstOld = new List<int> { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
var lstNew = new HashSet<int>(lstOld);
Console.WriteLine(string.Join(", ", lstNew));
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <set>
#include <vector>
template<typename _Ty>
inline const auto get_uniques(const std::vector<_Ty>& con) {
std::set<_Ty> set{ con.begin(), con.end() };
std::vector<_Ty> out{ set.begin(), set.end() };
return out;
}
template<typename _Ty>
inline const std::ostream& print(std::ostream& os_, const std::vector<_Ty>& con_) {
for (const auto& elem : con_)
std::cout << elem << " ";
std::cout << "\n";
return os_;
}
int main() {
std::vector<int> v{ 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
print(std::cout, v);
print(std::cout, get_uniques(v));
}
Lösung von: Jens Kelm (@JKooP)
// C++ 20 | VS-2022
#include <algorithm>
#include <iostream>
#include <vector>
const auto print(const auto& v) {
std::ranges::for_each(v, [](auto const& e) { std::cout << e << ' '; });
}
int main() {
std::vector<int> v{ 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
std::ranges::sort(v);
const auto& [first, last] = std::ranges::unique(v.begin(), v.end());
v.erase(first, last);
print(v);
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 1 |
Schwierigkeit: | k.A. |
Webcode: | gtmt-n6b2 |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |
Kommentare (1)