Reputation: 505
I have an array of Integer from which I want to return the index value of the smallest number appearence.
Integer[] array = {8,2,10,7,2,10};
So, in this case if I look for number 2, i find that 2 is contained in array[1]
and array[4]
. I'm a little bit lost in managing an array.
What I have done so far is to check if the given number exists
public int exist( Integer number ) {
int index = 0;
int position = 0;
while (position <= array.length && number == array[position]){
index = index + 1;
if( index <= array.length ){
index = position ;
} else {
index = -1;
}
}
return index ;
}
and I find from Internet this code which finds the first repeating element in an array of integers
class Main {
// This function prints the first repeating element in arr[]
static void printFirstRepeating(int arr[]) {
// Initialize index of first repeating element
int min = -1;
// Creates an empty hashset
HashSet<Integer> set = new HashSet<>();
// Traverse the input array from right to left
for (int i=arr.length-1; i>=0; i--) {
// If element is already in hash set, update min
if (set.contains(arr[i]))
min = i;
else // Else add element to hash set
set.add(arr[i]);
}
// Print the result
if (min != -1)
System.out.println("The first repeating element is " + arr[min]);
else
System.out.println("There are no repeating elements");
}
// Driver method to test above method
public static void main (String[] args) throws java.lang.Exception {
int arr[] = {10, 5, 3, 4, 3, 5, 6};
printFirstRepeating(arr); // output 5
}
}
I'm still unable to combine this code with the first to get what I really want.
Upvotes: 2
Views: 2674
Reputation: 159086
In your coded example 10, 5, 3, 4, 3, 5, 6
, the first repeated value is 3
at index 4, a repeat of the 3
at index 2. The other repeated value is 5
at index 5, a repeat of the 5
at index 1.
Illustrated:
10, 5, 3, 4, 3, 5, 6
↑ at index 4: 3 is first repeated value
↑ at index 2: 3 was first found here
↑ at index 5: 5 is second repeated value
↑ at index 1: 5 was first found here
So, possible results, depending on what you really want:
Here are solutions for all three:
private static void printIndexOfFirstRepeated(int ... values) {
Set<Integer> set = new HashSet<>();
for (int i = 0; i < values.length; i++)
if (! set.add(values[i])) { // add() returns false if value already in set
System.out.println("Index of first repeated: " + i);
return;
}
System.out.println("No repeat found");
}
private static void printIndexOfRepeatedFirst(int ... values) {
Map<Integer, Integer> mapValueToIndex = new HashMap<>();
for (int i = 0; i < values.length; i++) {
Integer prevIndex = mapValueToIndex.put(values[i], i); // put() returns old value, or null
if (prevIndex != null) {
System.out.println("Index of repeated first: " + prevIndex);
return;
}
}
System.out.println("No repeat found");
}
private static void printFirstIndexOfRepeated(int ... values) {
Integer firstIndex = null;
Map<Integer, Integer> mapValueToIndex = new HashMap<>();
for (int i = 0; i < values.length; i++) {
Integer prevIndex = mapValueToIndex.put(values[i], i); // put() returns old value, or null
if (prevIndex != null && (firstIndex == null || prevIndex < firstIndex))
firstIndex = prevIndex;
}
if (firstIndex != null)
System.out.println("First index of repeated: " + firstIndex);
else
System.out.println("No repeat found");
}
TEST
printIndexOfFirstRepeated(10, 5, 3, 4, 3, 5, 6);
printIndexOfRepeatedFirst(10, 5, 3, 4, 3, 5, 6);
printFirstIndexOfRepeated(10, 5, 3, 4, 3, 5, 6);
OUTPUT
Index of first repeated: 4
Index of repeated first: 2
First index of repeated: 1
Upvotes: 1
Reputation: 726509
One approach to solving this problem is to use a Map
instead of HashSet
.
Make a Map<Integer,Integer>
that maps a value from the array to the first occurrence of that number. Go through the array, and check the map if the value already exists.
The search code would look like this:
Map<Integer,Integer> firstAppearance = new HashMap<>();
for (int i = 0 ; i != arr.length ; i++) {
if (firstAppearance.containsKey(arr[i])) {
return firstAppearance.get(arr[i]);
} else {
firstAppearance.put(arr[i], i);
}
}
Upvotes: 2