I have one list method like below

    public static List<String> getDetails(String AccName){

listacNameList = new list(); List accList = new List([Select Id, Name from account where Name=:AccName]); for(account acc: accList ){ acNameList.add(acc.Name); } return acNameList ; }

in LWC i have input text where i need to show all Account but not duplicate
 <lightning-card  variant="narrow">
            <label class="slds-form-element__label">Select Account</label>
                <input data-id="inputFilter" onchange="" required value={accValues} name="input" list="filterList" placeholder="search account" />

                <datalist data-id="filterList" id="filterList" class="">
                    <template for:each={accDetails} for:item='item'>
                        <option key={item} value={item}>{item}</option>

accDetails = [];
getDetails({AccName: this.value})
    this.accDetails= result;

there are chances that i will get duplicate account Name in input filter which i dont want, i am not able to remove duplicate account Name from result. Please suggest

  • What worries me here is that while accounts may not have unique names they represent different accounts still.
    – Phil W
    Nov 17, 2021 at 9:32
  • its just the example i have provided above.
    – Sandy
    Nov 17, 2021 at 9:42

1 Answer 1


If you really do want to only consider a subset of the Accounts, based on uniqueness of the name, but you still want to allow non-unique account names, you can easily do this either in the Apex or in the JS. The principle in both cases is to set up a map from name to Account then use the map values.

In the Apex you would do something like:

public static List<Account> getDetails(String AccName){
    List<Account> accList = new List<Account>([Select Id, Name from account where Name=:AccName]);
    Map<String, Account) accountsByName = new Map<String, Account>();

    for (Account account : accList) {
        accountsByName.put(account.Name, account);

    return accountsByName.values();

Note that I corrected the return type for the Apex based on your example code. Also note that the last "duplicate" Account is the one that is retained since I always add all the Account records to the map (overwriting any existing match for the name). This is case sensitive on the names, but you could make it case insensitive very easily.

You could use the following to convert an array of non-unique strings to an array of unique strings in JavaScript:

const uniqueAccountNames = [...new Set(nonUniqueAccountNames)];
  • Hi Phil..I want to make it in Javascript.. to avoid any performance issue.i am avoiding it in apex class
    – Sandy
    Nov 17, 2021 at 9:42
  • I am returning string of account name not object.sorry i missed that code to add in here
    – Sandy
    Nov 17, 2021 at 9:46
  • Is performance really an issue? How many accounts do you have? Regardless, the same approach applies in both cases. Turn the array (just names isn't a great idea BTW since you don't then know the related ID) into a map (in JS, that is just an object with properties) then get the properties back into an array. You will find plenty of help on the "how" by doing an Internet search.
    – Phil W
    Nov 17, 2021 at 9:50
  • it not account,but some other data and i cant change the existing apex return type from list<string> to map as this method reffered some other places as well.Could you please help me to provide an example to get result and remove duplcate and place in new array to display
    – Sandy
    Nov 17, 2021 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .