I have a list that includes many strings. Basically I want to print out the position of the string when it is unique. My code works, but the problem is that it takes too long time. I got recommended to use a HashMap which I did, but here the memory was costed. Is there any "middle path" way of doing it without causing too much trouble for the speed and the memory?
So if list = ["ABC", "456", "ABC", "Z"] --> the output should be 1 because "456" is the first unique element and the position is 1.
Is there anyway of when I have found two occurrence of one string, to maybe overlook all the elements with the same value? Or another better way of doing it without causing too much damage on both speed/memory.
enter code here
ArrayList<String> strings = new ArrayList<>();// many items inside
for (int i = 1; i < strings.size(); i++) {
String next = strings.get(i);
if (Collections.frequency(strings,next) == 1) {
System.out.println(i);
break;
}
}