Page 1 of 1

Trouble with: Procedure RemoveListDuplicates()

Posted: Wed Apr 24, 2024 9:36 pm
by millie78526
TIA !
Trouble with: Procedure RemoveListDuplicates()
Sorry having BrainFog :

Code: Select all

;EnableExplicit

Global NewList WordsList.s() , NewList Index_List.s() 

; Directly add words to the list using AddElement
AddElement(WordsList())
WordsList() = "Apple"
AddElement(WordsList())
WordsList() = "apple"
AddElement(WordsList())
WordsList() = "Apple"
AddElement(WordsList())
WordsList() = "Banana"
AddElement(WordsList())
WordsList() = "Banana"
AddElement(WordsList())
WordsList() = "banana"
AddElement(WordsList())
WordsList() = "apple"  ; Intentional case-insensitive duplicate
AddElement(WordsList())
WordsList() = "Carrot"
AddElement(WordsList())
WordsList() = "Banana"  ; Exact duplicate

; Sort the list to facilitate duplicate removal
SortList(WordsList(), #PB_Sort_Ascending | #PB_Sort_NoCase)

; Function to remove duplicates from a sorted list
Procedure RemoveListDuplicates()
  If ListSize(WordsList()) > 0  ; Only process if there is at least one element
    Define previousLine.s = "zzzzzzzz"
    ForEach WordsList()
      If previousLine = WordsList()
        DeleteElement(WordsList(), ListIndex(WordsList()))  ; Remove the duplicate element
        NextElement(WordsList()) 
      EndIf 
      If previousLine <> WordsList()  ; Update previous line
        Debug "WordsList() = " + ListSize(WordsList())
        Debug "ListSize() = " + ListSize(Index_List())
        AddElement(Index_List())
        Index_List() = WordsList() 
      EndIf
    Next
  EndIf
EndProcedure

; Remove duplicates
RemoveListDuplicates()

; Function to print the list
Procedure PrintWordsList()
  ForEach WordsList()
    Debug "Word: " + WordsList()
  Next
  ForEach Index_List()
    Debug "Word: " + Index_List()
  Next
  
EndProcedure

; Print the words in the list
PrintWordsList()

; Clean up
FreeList(WordsList())
MessageRequester("End of Program", "The program has completed execution.")


Re: Trouble with: Procedure RemoveListDuplicates()

Posted: Wed Apr 24, 2024 10:10 pm
by pjay
I would use a map for this, you wouldn't need to sort the list either:

Code: Select all

Procedure RemoveListDuplicates(casesensitive = #True)
  Protected lcword.s : NewMap st.i()
  If ListSize(WordsList()) > 0  ; Only process if there is at least one element
    ForEach WordsList()
      lcword = WordsList() : If casesensitive = #False : lcword = LCase(lcword) : EndIf
      If st(lcword) = 0 : st() = 1 : Else : DeleteElement(WordsList()) : EndIf
    Next
  EndIf
  FreeMap(st())
EndProcedure

Re: Trouble with: Procedure RemoveListDuplicates()

Posted: Wed Apr 24, 2024 10:48 pm
by AZJIO

Code: Select all

Procedure RemoveListDuplicates(CaseSensitive = #True)
	Protected w$
	Protected NewMap Unique()
	ForEach WordsList()
		If CaseSensitive
			If FindMapElement(Unique(), WordsList())
				DeleteElement(WordsList())
			Else
				AddMapElement(Unique(), WordsList())
			EndIf
		Else
			w$ = LCase(WordsList())
			If FindMapElement(Unique(), w$)
				DeleteElement(WordsList())
			Else
				AddMapElement(Unique(), w$)
			EndIf
		EndIf
	Next
EndProcedure

Re: Trouble with: Procedure RemoveListDuplicates()

Posted: Thu Apr 25, 2024 3:31 am
by millie78526
Thank you pjay :
My code is part of a much larger program that Reads in .txt files , often over 5k lines of text ,
and then breaks lines into words , then removes Duplicates .
Then creates a ListView of Unique words ,
User can then click on Resort_Button to Resort Words according to CheckBox Ascending or Descending .
Could you explain what's going on here? :

Code: Select all

      If st(lcword) = 0  
        st() = 1  

Thanks

Re: Trouble with: Procedure RemoveListDuplicates()

Posted: Thu Apr 25, 2024 5:14 am
by AZJIO

Re: Trouble with: Procedure RemoveListDuplicates()

Posted: Thu Apr 25, 2024 7:13 am
by infratec

Code: Select all

Procedure RemoveListDuplicates()
  If ListSize(WordsList()) > 0  ; Only process if there is at least one element
    Define previousLine.s = "zzzzzzzz"
    ForEach WordsList()
      If LCase(previousLine) = LCase(WordsList())
        DeleteElement(WordsList())  ; Remove the duplicate element
        ;NextElement(WordsList()) 
      EndIf 
      If previousLine <> WordsList()  ; Update previous line
        Debug "WordsList() = " + ListSize(WordsList())
        Debug "ListSize() = " + ListSize(Index_List())
        AddElement(Index_List())
        Index_List() = WordsList() 
      EndIf
      previousLine = WordsList()
    Next
  EndIf

EndProcedure

Re: Trouble with: Procedure RemoveListDuplicates()

Posted: Thu Apr 25, 2024 8:26 pm
by Demivec
millie78526 wrote: Thu Apr 25, 2024 3:31 am Could you explain what's going on here? :

Code: Select all

      If st(lcword) = 0  
        st() = 1  
St() is a map that is indexed by strings and contains integers. If the map element at index lcword equals zero then it is set equal to one. This condition is only met when the map element is first created. As a result when the same map element is referenced again its value is now 1 and this signals a duplicate word and so the current word entry is deleted from WordsList().

Re: Trouble with: Procedure RemoveListDuplicates()

Posted: Sat Apr 27, 2024 12:06 am
by millie78526
Thanks Guys ,
You have given me a lot to work with .
Thanks for the lesson on Map .
I have never worked with Map before .
Thanks again .