So I have an Array A of N size containing positive Integers. the array could have many duplicates, and I want to find the shortest distance to travel the array and visit each number that occurs in the array
what is the best way to iterate the array starting at A[0] and so on.
So far I have come up with adding all the numbers to a set so I can compare if I have been there already.
for example in the following array the shortest distance to visit all numbers is 5
Integer[] nums = { 2, 6, 7, 2, 3, 3, 1, 2 };
so I add them to a hashset
Set<Integer> UniqueNums = new HashSet<>(Arrays.asList(nums));
this is where I get confused I know the following is wrong and its only psudocode but i would do something along the lines of
int count = 0;
for(int i : nums )
{
if(UniqueNums.contains(i)
{
count ++;
}
}
nums
– Ele Dec 3 '17 at 1:161, 2, 3, 6, 7
), which isint count = new HashSet<>(Arrays.asList(nums)).size();
– Andreas Dec 3 '17 at 1:19