2

I need to sort an array and get indexes linked to an unsorted array. The problem is that if the unsorted array contains duplicate entries, i.e. [1,1,1,2,2,], then indexes are the same for these entries. For the example [3,5,5,3,3,] the indexes would be [0,1,1,0,0]. But I would need to get the following indexes [0,3,4,1,2]. How to do this?

ArrayList<Double> nfit = new ArrayList<Double>();
ArrayList<Double> nfit_copy = new ArrayList<Double>(nfit);

// Fill nfit

Collections.sort(nfit);

int[] ind = new int[nfit.size()];

for (int n = 0; n < nfit.size(); n++){
    ind[n] = nfit_copy.indexOf(nfit.get(n));
}

3 Answers 3

1

This is because you use nfit_copy.indexOf(nfit.get(n)); which will gave you the same index if you have duplicated number

For Example

[3,5,5,3,3,]----> every time you will use :nfit.indexOf(3) ,will give you index 0

so , may be you need to change this value or set it null (Don't reomve it because it will change the index) , to allow you to get the next duplicate number

Try this:

ArrayList<Integer> nfit = new ArrayList<Integer>();
ArrayList<Integer> nfit_copy = new ArrayList<Integer>(nfit);

// Fill nfit 

  nfit.add(3);
  nfit.add(5);
  nfit.add(5);
  nfit.add(3);
  nfit.add(3);


  nfit_copy = (ArrayList<Integer>) nfit.clone();


  Collections.sort(nfit);

  int[] ind = new int[nfit.size()];

  for (int n = 0; n < nfit.size(); n++) {
         ind[n] = nfit_copy.indexOf(nfit.get(n));
         nfit_copy.set(nfit_copy.indexOf(nfit.get(n)), null);
   }
   for (int i = 0; i < ind.length; i++) {
         int j = ind[i];
         System.out.println(j);
    }
Sign up to request clarification or add additional context in comments.

5 Comments

It provides an incorrect result for nfit.add(3); nfit.add(3); nfit.add(5); nfit.add(3); nfit.add(3);. The result is 0 1 3 4 2, instead of 0 1 4 2 3.
How 0 1 4 2 3 ??you have number 3 in index (0 ,1 ,3 ,4 ) and one 5 in index 2 , so please check it again :)
Probably I misunderstood your idea. I checked the code of Tee Gee (see below), and it provides 0 1 4 2 3 for the array [3,3,5,3,3], which seems to be correct to me.
0 1 4 2 3 will mean your array is 3 3 3 5 3 after sorted , so this is not sorted index you want , and seem incorrect to me :)
We were talking about different things. In your case you assign indexes to unsorted array (and sorry, this is exactly what I need), but Tee Gee provides sorted indexes.
1
public static void main(String[] args) {
        Integer[] input = new Integer[]{1,2,2,1,5,6,2,3,2,3,4,5,6,1};
        List<Integer> mappedIndexes = new ArrayList<Integer>();

        List<Integer> sorted = new ArrayList<Integer>();
        for (int num : input) {
            sorted.add(num);
        }       
        Collections.sort(sorted);

        for (int number : input) {
            System.out.println("finding for input -> " + number);
            for (int i = 0; i < sorted.size(); i++) {
                int sortedNumber = sorted.get(i);
                if (number == sortedNumber) {
                    if (mappedIndexes.contains(i)) {
                        continue;
                    } else {
                        System.out.println("setting index as -> " + i);
                        mappedIndexes.add(i);
                        break;
                    }
                }
            }
        }
        System.out.println(mappedIndexes);
    }

Comments

1

Here is my solution with Set and HashMap structures. Read the code comments for more info, please.

int[] input = { 45, 3, 4, 9, 2, 1, 45, 3 };
// Backup the initial array for later use.
int[] originalArray = Arrays.copyOf(input, input.length);    
Arrays.sort(input);

// Determine all occurences with a set.
Set<Integer> set = new HashSet<Integer>();
for (int i : input) {
    set.add(i);
}

// Populate a hashmap for keeping <value,index> pairs.
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int counter = 0;
for (int i : set) {
    map.put(i, counter++);
}

// Populate the output array.
int[] output = new int[input.length];
for (int i = 0; i < output.length; i++) {
    output[i] = map.get(originalArray[i]);
}

That's all. If you print the contents of output to console, you can see the results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.