-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove_duplicates.pl
More file actions
23 lines (19 loc) · 1.81 KB
/
Copy pathremove_duplicates.pl
File metadata and controls
23 lines (19 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
%****************************************************************************************************
% *
% Predicate to Remove Duplicate Characters from a String *
% *
% The following Prolog code defines a predicate to remove duplicate characters from a string, *
% retaining only the first occurrence of each character. *
% *
% - `remove_duplicates/2`: Entry point to remove duplicates from a string. *
% *
% Example usage: *
% ?- remove_duplicates("banana", Result). *
% Result = "abn" *
% *
%****************************************************************************************************
% ----------------------------------------Predicate to remove duplicate characters from a string----------------------------------------
remove_duplicates(String, Result) :-
string_chars(String, CharList), % Convert string to list of characters
sort(CharList, SortedList), % Sort and remove duplicates
string_chars(Result, SortedList). % Convert the list back to a string