unexpected results
Zamiast skupiać się na prostocie i wydajności nalezy pochylić się nad funkcjonalnością
Kotlin
1 2 3 4 5 2 6 7 2 9 2
[1, 2, 4, 5, 2, 7, 2, 2] o.k.
C++
1 2 3 4 5 2 6 7 2 9 2
[1, 2, 4, 5, 2, 7, 2, 2] o.k.
Kotlin
1 2 3 4 5 2 6 7 2 9 2 10 20 2 2 2 2 18 2 33 121
[1, 2, 4, 5, 2, 7, 2, 2, 20, 2, 2, 121] difference
C++
1 2 3 4 5 2 6 7 2 9 2 10 20 2 2 2 2 18 2 33 121
[1, 2, 4, 5, 2, 7, 2, 2, 20, 2, 2, 18, 2, 121] difference
Kotlin
1 2 2 2 2 3 4 5 2 6 7 2 9 2 2 2 2 2 2 1 10
[1, 4, 5, 2, 7, 2, 2] difference
C++
1 2 2 2 2 3 4 5 2 6 7 2 9 2 2 2 2 2 2 1 10
[1, 2, 2, 3, 4, 5, 2, 7, 2, 2, 2, 2, 1, 10 ] difference
Kotlin
6 1 6 3 4 5 6 6 7 6 9 6 10 20 6 6 6 33 1
[6, 4, 5, 6, 6, 6, 20, 6, 1] difference
C++
6 1 6 3 4 5 6 6 7 6 9 6 10 20 6 6 6 33 1
[6, 6, 4, 5, 6, 7, 6, 6, 20, 6, 6, 1] difference
Kotlin
1 33 20 1 21 1 1 0
[1, 20, 1, 1] difference
C++
1 33 20 1 21 1 1 0
[1, 20, 1, 1, 0] difference
Nie znam języka "java" ani "kotlin" ale napisałem sobie w C++
i testowałem
dla Kotlin link Online Compilers Kotlin
dla C++ link Online Compilers C++
testy dla Pythona nie przeprowadzałem ponieważ kod z zamieszczonego powyżej listingu zwyczajnie dostaje crash
Chciałbym usunąć numery które występują po duplikatach (w tej liście duplikatem jest liczba 2, ponieważ występuje więcej niż 1 raz)
Moja sugestia
umownie nazwijmy liczbę najczęściej powtarzającą się "wartownikiem"
osobiście bym go nie usuwał
dla testu w programie który napisałem w C++ należy za komentować
Kopiuj
if( (*p == xElement) && (*r != xElement) ) // <--odkomentować
//if( (*p == xElement) ) // <-- zakomentowac
wynik dla przykładowego testu
1 2 2 2 2 3 4 5 2 6 7 2 9 2 2 2 2 2 2 1 10
[1, 2, 2, 2, 2, 4, 5, 2, 7, 2, 2, 2, 2, 2, 2, 2, 10]
Poniżej pełne listingi na jakich testowałem
poniżej algorytm user @watpliwosci
Kopiuj
fun getList(list: MutableList<Int>): MutableList<Int>
{
val doubledIndexes = mutableSetOf<Int>()
val listWithoutDoubled = mutableListOf<Int>()
list.forEachIndexed { index, item ->
if (!listWithoutDoubled.contains(item)) {
listWithoutDoubled.add(item)
} else {
doubledIndexes.add(index)
doubledIndexes.add(list.indexOf(list.find { it == item }))
}
}
val sorted = doubledIndexes.sortedDescending()
sorted.forEach {
if (list.getOrNull(it + 1) != null) {
list.removeAt(it + 1)
}
}
return list
}
fun main(args: Array<String>) {
//val input = mutableListOf(1, 2, 3, 4, 5, 2, 6, 7, 2, 9, 2)
//val input = mutableListOf(1, 2, 3, 4, 5, 2, 6, 7, 2, 9, 2, 10, 20, 2, 2, 2, 2, 18, 2, 33, 121)
//val input = mutableListOf(1, 2, 2, 2, 2, 3, 4, 5, 2, 6, 7, 2, 9, 2, 2, 2, 2, 2, 2, 1, 10)
//val input = mutableListOf(6, 1, 6, 3, 4, 5, 6, 6, 7, 6, 9, 6, 10, 20, 6, 6, 6, 33, 1)
val input = mutableListOf(1, 33, 20, 1, 21, 1, 1, 0)
for(el in input)
{
print("$el ")
}
println()
print(getList(input))
}
poniżej program napisany przeze mnie dla testu
Kopiuj
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <limits> //std::numeric_limits
using namespace std;
int main()
{
//vector<int>v = {1, 2, 3, 4, 5, 2, 6, 7, 2, 9, 2}; //output (1, 2, 4, 5, 2, 7, 2, 2)
//vector<int>v = {1, 2, 3, 4, 5, 2, 6, 7, 2, 9, 2, 10, 20, 2, 2, 2, 2, 18, 2, 33, 121};
//vector<int>v = {1, 2, 2, 2, 2, 3, 4, 5, 2, 6, 7, 2, 9, 2, 2, 2, 2, 2, 2, 1, 10};
//vector<int>v = {6, 1, 6, 3, 4, 5, 6, 6, 7, 6, 9, 6, 10, 20, 6, 6, 6, 33, 1};
vector<int>v = {1, 33, 20, 1, 21, 1, 1, 0};
int nCount = 0, xElement = *(v.begin());
for(auto & el: v)
{
cout << el << " ";
uint16_t c = count(v.begin(), v.end(), el);
if(c > nCount)
{ nCount = c;
xElement = el;
}
}
cout << "\n";
std::vector<int>::iterator p, r;
p = v.begin(); r = v.begin()+1;
uint16_t s = v.size();
for(uint16_t i = 0; i < s-1; ++i)
{
//if( (*p == xElement) && (*r != xElement) ) //wartownik to wartownik
if( (*p == xElement) )
{
//cout << "ptr-> " << *r << "\n"; //for test
p = v.erase(r);
--s;++r;
} else {
++p;++r;
}
}
cout << "[";
for(auto & el: v)
{
cout << el << ", ";
}
cout << "]\n";
return 0;
}