Reputation: 169
private static boolean moreThanOnce(ArrayList<Integer> list, int number) {
if (list.contains(number)) {
return true;
}
return false;
}
How do I use list.contains
to check if the number was found in the list more than once? I could make a method with for
loop, but I want to know if it's possible to do using .contains
. Thanks for help!
Upvotes: 6
Views: 733
Reputation: 78995
Other answers are great because they explain the efficient ways to do it but the sole purpose of this solution is to show how List::contains
can be used to solve this problem which is the specific requirement of the question. Please also check the following comment from OP requesting for it.
Thank you, do you think it's possible to do with .contains only? The other user commented "indexOf".
It's not possible with List::contains
alone but you can combine List::contains
with some other functions of List
and make it work.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Tests
List<Integer> list = List.of(10, 20, 10, 30);
System.out.println(moreThanOnce(new ArrayList<>(list), 10));
System.out.println(moreThanOnce(new ArrayList<>(list), 20));
}
private static boolean moreThanOnce(List<Integer> list, int number) {
if (list.size() == 1) {
return false;
}
if (list.get(0) == number && list.subList(1, list.size()).contains(number)) {
return true;
}
return moreThanOnce(list.subList(1, list.size()), number);
}
}
Output:
true
false
Upvotes: 2
Reputation: 51393
Others already point out the problem. However, typically you could use the Collection.frequency method:
private static boolean moreThanOnce(ArrayList<Integer> list, int number) {
return Collections.frequency(list, number) > 1;
}
Upvotes: 2
Reputation: 140309
You can't do it just with contains
. That just tests if the item is anywhere in the list.
You can do it with indexOf
, though. There are two overloads of indexOf
, one of which allows you to set a position in the list to start searching from. So: after you find one, start from one after that position:
int pos = list.indexOf(number);
if (pos < 0) return false;
return list.indexOf(number, pos + 1) >= 0;
Or replace the last line with:
return list.lastIndexOf(number) != pos;
If you want a more concise way (although this iterates the whole list twice in the case that it's not found once):
return list.indexOf(number) != list.lastIndexOf(number);
Upvotes: 5
Reputation: 393771
You can use Stream
s:
private static boolean moreThanOnce(ArrayList<Integer> list, int number) {
return list.stream()
.filter(i -> i.equals (number))
.limit(2) // this guarantees that you would stop iterating over the
// elements of the Stream once you find more than one element
// equal to number
.count() > 1;
}
Upvotes: 5
Reputation: 1052
If you wish to count how many times it exist in the list
int times = list.stream().filter(e -> number==e).count();
Upvotes: 3
Reputation: 613
The method add of Set returns a boolean whether a value already exists (true if it does not exist, false if it already exists).
Iterate all the values and try to add it again.
public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)
{
final Set<Integer> setToReturn = new HashSet<>();
final Set<Integer> set1 = new HashSet<>();
for (Integer yourInt : listContainingDuplicates)
{
if (!set1.add(yourInt))
{
setToReturn.add(yourInt);
}
}
return setToReturn;
}
Upvotes: 2