-2

I have a list of countries where the product is available. I am looking for the code to check whether any duplicate value exists in the list of countries.

They are giving like this: AZ,CA,GB, AU,AR,AT,MX,NL,NZ. How do I check in Selenium if a duplicate value exists with a loop?

rgettman
  • 176,041
  • 30
  • 275
  • 357
Saad Hanif
  • 1
  • 1
  • 1
  • 4
    It is unclear how selenium is part of this problem. What have you tried so far? Please include a piece of code that shows how you use selenium in this. – luksch Jun 17 '15 at 20:32

2 Answers2

1

As others have pointed out, this has very little to do with Selenium, other than where your values come from.

I will assume that your list of countries comes from a pulldown menu. If not you will need to adjust the code below to match.

import java.util.*;

...
Select slctCountry = new Select(driver.findElement(By.id("select_id")));
// create an empty List
List<String> optionsList = new ArrayList<String>();
// a Set naturally removes duplicates!
Set<String> optionsSet = Collections.emptySet();
for (WebElement option : slctCountry.getOptions()) {
    // fill both from the same source
    optionsList.add(option.getText());
    optionsSet.add(option.getText());
}
// compare the two
Assert.assertEquals("The List contains duplicates!", optionsSet.size(), optionsList.size());
SiKing
  • 10,003
  • 10
  • 39
  • 90
0

A modified version of this. If the array of String contains duplicates it will simply return true.

String[] array = {"a", "b", "c", "a", "b"};
        ArrayList<String> str = new ArrayList<String>();
        for (String s:array){
            str.add(s);
        }
        boolean ind = false;
        for (int i = 0; i < array.length; i++) {
            str.remove(array[i]);
            for (int j = 0; j < str.size(); j++) {
                if (array[j].equals(str.get(j))){
                    System.out.println(str.get(j)  +" "+ array[j] );
                    ind = true;
                }
            }
        }

Note:This probably has nothing to do with Selenium, at least the way you showed in the question.

Community
  • 1
  • 1
Saifur
  • 16,081
  • 6
  • 49
  • 73