finding the duplicated values in List of String Dart - list

i have the following list with duplicate values and i need to check if the values is duplicated print it else skip it
List lst = ["AA","BB","BBB","AA"];
what should printed is :
AA
Thanks ;

Would this work for you?
List list = ["AA", "BB", "BBB", "AA"];
List distinctList = list.toSet().toList();
void main() {
for (int i = 0; i < distinctList.length; i++) {
list.remove(distinctList[i]);
}
//// or you could use a "for in" like this:
// for (var item in distinctList) {
// list.remove(item);
// }
print(list.toSet().toList());
}
Or using forEach() on the Set
List list = ['AA', 'BB', 'BBB', 'AA'];
void main() {
list.toSet().forEach((item) => {list.remove(item)});
print(list.toSet().toList());
}

List lst = ["AA","BB","BBB","AA"];
int duplicateDetectedCount = -1;
String duplicateItem = "";
for(String item in lst){
for(String innerItem in lst){
if(item == innerItem){
duplicateDetectedCount++;
if(duplicateDetectedCount == 1){
duplicateItem = innerItem;
}
}
}
}
print(duplicateItem); // prints AA

lst.where((e) => lst.where((element) => element == e).length > 1).toSet().toList();
One Liner

Related

Merge two lists of objects in Dart

I have objects which have a parameter name and a parameter counter. Those objects are stores inside a list. Some of the items in my list have the duplicate parameter "name". I want to remove the duplicates inside the list and add the counter of that duplicate to the duplicate-objects parameter.
class Person{
Person({this.name, this.counter)};
String name;
int counter;
}
List<Person> theList = [];
theList.add(Person(name:"Ben", counter: 2);
theList.add(Person(name:"Ben", counter: 5);
//I need a function that changes the List to show Ben with counter 7
class Person{
final String name;
int counter;
Person({required this.name, required this.counter});
}
extension on List<Person> {
void addPerson({required String name, required int counter}) {
if (isNotEmpty) {
try {
var person = firstWhere((p) => p.name == name);
person.counter += counter;
} catch(e) {
add(Person(name:name, counter: counter));
}
} else {
add(Person(name:name, counter: counter));
}
}
}
void main() {
var theList = <Person>[];
theList.addPerson(name:"Lucho", counter: 4);
theList.addPerson(name:"Ben", counter: 2);
theList.addPerson(name:"Ben", counter: 5);
print(theList);
print(theList.length);
print("${theList[1].name} - ${theList[1].counter}");
}
Result:
[Instance of 'Person', Instance of 'Person']
2
Ben - 7
first define a class for a list of Persons:
class PersonList{
List<Person> _personList = [];
get personList{
return [..._personList];
}
void addToList(Person person){
for(int i=0; i<_personList.length; i++){
if(_personList[i].name == person.name){
_personList[i].counter = _personList[i].counter + person.counter;
return;
}
}
_personList.add(person);
return;
}
}
then, you can create a new instance of it and add some entries to the list:
PersonList theList = new PersonList();
theList.addToList(Person(name:"Ben", counter: 2));
print(theList.personList[0].counter);
theList.addToList(Person(name:"Ben", counter: 5));
print(theList.personList[0].counter);
feel free to only use the addToList method.

Unity Iterate over a list to sort by the number value

I have a text file called highscore.txt and I know the format of the data going into the file which works in the following way:
Username:score
I then need to search through the whole file to see where the new score needs to be added such that the file is in order of score from largest to smallest
List<string> tempoaryList = new List<string>();
List<int> valueList = new List<int>();
string lineValues = "";
using (StreamReader sr = new StreamReader(finalPath))
{
while (sr.EndOfStream == false)
{
lineValues = sr.ReadLine();
tempoaryList.Add(lineValues);
int data = Convert.ToInt32(lineValues.Substring(lineValues.LastIndexOf(':') + 1));
valueList.Add(data);
}
}
valueList.Sort();
for(int i =0; i > valueList.Count; i++)
{
if(valueList[i] <= playerScore)
{
tempoaryList.Insert(i - 1, playerScore.ToString());
}
}
using (StreamWriter sw = new StreamWriter(finalPath, false))
{
for (int i = 0; i < tempoaryList.Count; i++)
{
sw.WriteLine(tempoaryList[i]);
Debug.Log(tempoaryList[i]);
}
}
The above just simply doesn't change the text file and leaves it as it was found, any ideas of what I am doing wrong?
You should change your code to this:
ScoreList.cs
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
public class ScoreList : MonoBehaviour {
private void Start() {
GameOver.GameOverEvent += UpdateScoreBoardFile;
}
private void UpdateScoreBoardFile(string playerName, int playerScore) {
string lineValues;
string finalPath = "[ScoreBoard File Location]";
List<string> tempoaryList = new List<string>();
List<int> valueList = new List<int>();
bool isScoreLowest = true;
using (StreamReader sr = new StreamReader(finalPath)) {
while (sr.EndOfStream == false) {
lineValues = sr.ReadLine();
tempoaryList.Add(lineValues);
int data = Convert.ToInt32(lineValues.Substring(lineValues.LastIndexOf(':') + 1));
valueList.Add(data);
}
}
if (tempoaryList != null) {
for (int i = 0; i < valueList.Count; i++) {
if (valueList[i] <= playerScore) {
tempoaryList.Insert(i, playerName + ":" + playerScore.ToString());
isScoreLowest = false;
break;
}
}
}
if (isScoreLowest) {
tempoaryList.Add(playerName + ":" + playerScore.ToString());
}
using (StreamWriter sw = new StreamWriter(finalPath, false)) {
for (int i = 0; i < tempoaryList.Count; i++) {
sw.WriteLine(tempoaryList[i]);
Debug.Log(tempoaryList[i]);
}
}
}
}
GameOver.cs (or wherever you manage the game over condition):
using UnityEngine;
using System;
public class GameOver : MonoBehaviour {
public static event Action<string, int> GameOverEvent;
string playerName;
int finalScore;
private void GameOver() {
//Do other stuff when the game is over
GameOverEvent(playerName, finalScore);
}
}
Explanation of the changes:
isScoreLowest is needed to Add the new lowest score at the end of the list, since the for loop won't add it by itself.
The if (tempoaryList != null) check is done to skip the for loop when the scoreboard is empty.
And you saved yourself from doing a Sort, which is always a good thing. :)

How to count duplicates in List<Integer>

Duplicate of Count occurrences of words in ArrayList
i trying to counts duplicates of my JSON response.
Im got values from 1,2 ... to 13,14
First what i do is sort the list:
Collections.sort(calues);
every value can be used 4 times.
So at last i have list with 1,1,1,1,2,2,2,2.....14,14,14,14
I need to count repeated values and when any value like 1,2 etc will be repeated 3 times i need to give message to user.
How i can do this?
Thanks for help :)
Going with tutorial i create this:
public int count(List<Integer> nums) {
boolean inclump = false;
int clumpcnt = 0;
for (int i = 1; i < nums.size(); i++) {
if (Objects.equals(nums.get(i), nums.get(i - 1))) {
if (!inclump) {
inclump = true;
clumpcnt++;
if(clumpcnt ==3){
Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
}
}
} else {
inclump = false;
}
}
return clumpcnt;
}
but this count the values and when values like 2,2 and 3,3 and 4,4 then this function enter
if(clumpcnt ==3){
Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
}
I declared a list private List<Integer> cardsValues = new ArrayList<>();
Then i added values here:
first 5 on start application:
#Override
public void onNext(#NonNull Cards cards) {
cardsArray = cards.getArrayCards();
for (Card item : cardsArray) {
imgUrls.add(item.getImage());
cardsValues.add(item.getRank());
}
}
And the on every click with "Get new Cards"
Im adding new value with this same method
for (Card item : cardsArray) {
imgUrls.add(item.getImage());
cardsValues.add(item.getRank());
}
And here i call the method
private void checkDuplicateAchievement() {
Collections.sort(cardsValues);
count(cardsValues);
}
Here is my full method
private void getCards(final String deck_id, final int count) {
cardApi.getCards(deck_id, count)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Cards>() {
#Override
public void onSubscribe(#NonNull Disposable d) {
}
#Override
public void onNext(#NonNull Cards cards) {
cardsArray = cards.getArrayCards();
for (Card item : cardsArray) {
imgUrls.add(item.getImage());
cardsValues.add(item.getRank());
}
cardsRecyclerView.notifyDataSetChanged(); recyclerView.smoothScrollToPosition(cardsRecyclerView.getItemCount());
checkDuplicateAchievement();
}
#Override
public void onError(#NonNull Throwable e) {
}
#Override
public void onComplete() {
}
});
}
You can make use of a HashMap like below.
** Not tested , typing java after a long time!, Just to give you an idea.
public int count(List<Integer> nums) {
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
for (int i = 1; i < nums.size(); i++) {
if(countMap.get(nums[i]) != null){
countMap.set(i, countMap.get(nums[i]) + 1)
if (countMap.get(i) >=3){
Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
}
}else{
countMap.set(i, 1)
}
}
}
Try this:
public int count(List<Integer> nums) {
boolean inclump = false; // Not used
int clumpcnt = 0;
int value = nums.get(0);
for (int i = 1; i < nums.size(); i++) {
if (Objects.equals(value, nums.get(i))) {
clumpcnt++;
if (clumpcnt == 3) {
Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
Log.d("COUNT", "WOW VALUE " + String.valueOf(value) + " REPEATED 3 TIMES, YOU WIN");
clumpcnt = 0;
}
} else {
clumpcnt = 0;
value = nums.get(i);
}
}
return clumpcnt;
}
FYI, nums list contains 1,1,1,1,2,2,2,2,3,3,3,3
OUTPUT LOG:
28078-28078/com.ferdous.stackoverflowanswer D/COUNT: WOW VALUE 1 REPEATED 3 TIMES, YOU WIN
28078-28078/com.ferdous.stackoverflowanswer D/COUNT: WOW VALUE 2 REPEATED 3 TIMES, YOU WIN
28078-28078/com.ferdous.stackoverflowanswer D/COUNT: WOW VALUE 3 REPEATED 3 TIMES, YOU WIN
You can sort your list and then loop the sorted elements and compare values at adjacent positions.If they match then duplicates are present

QListWidget Insert Items

I am trying to add two items into my QListWidget dynamically. However, the following codes only allow me to add only the last item into the list. strList.size() contains 4 items. Assuming name contains "ABC 1" and "ABC 2".
Is my loop incorrect? Or is my method of adding items into the listWidget wrong?
.h:
public:
QListWidgetItem *item[2];
.cpp:
...
while(!xml.atEnd())
{
xml.readNextStartElement();
if(xml.isStartElement())
{
if(xml.name() == "OS")
{
strList << xml.readElementText();
}
}
}
int num = 0;
for(int i = 0; i < strList.size(); i++)
{
if(strList[i] == "ABC")
{
QString name = strList[i] + strList[i+1];
item[num] = new QListWidgetItem();
item[num]->setText(name);
ui.listWidget->insertItem(num, item[num]);
num += 1;
}
}
Output (listWidget):
ABC02
Expected output (listWidget):
ABC01 ABC02

Delete items of MultiSelectList in Windows phone 7

how can i delete items in multiselectlist
My code's working not correctly
for (int i = MyListBox.Items.Count - 1; i >= 0; i--)
//for (int i = -1; i <= MyListBox.Items.Count; i++)
{
if (MyListBox.IsSelectionEnabled == true)
{
MyObservable.RemoveAt(i);
}
}
MyListBox : multiSelectList
MyObservable : ObservableCollection<>
you are unable to remove the items because you are altering the collection while trying to use the collection. You need to gather the items you want to delete, then delete them as such:
ICollection<Item> selectedItems = new List<Item>(MyListBox.SelectedItems.Count);
foreach (var item in MyListBox.SelectedItems)
{
Item myItem = item as Item;
if (myItem == null) continue;
selectedItems.Add(myitem);
}
foreach (var item in selectedItems)
{
MyObservable.Remove(item);
}
MyListBox.IsSelectionEnabled = false;