Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

i have an array which contains duplicate values. now i want an other array of the indexes of the array with all the index of non repeat element + latest index of the duplicate items.

int [] arr = {1,1,2,2,3,4,5,6,6,7};

output

int[] indexarr = {1,3,4,5,6,8,9}

where 1 is the index of second 1,3 is the index of last 2, 4 is the index of 3 etc..
Posted
Comments
_Asif_ 10-Jul-14 8:02am    
What have you tried so far?
Maciej Los 10-Jul-14 8:11am    
Why to enumerate index: 4, 5 and 9, if their corresponding values 4, 5 and 7 are not duplicated?
ravikhoda 10-Jul-14 8:40am    
coz i need index of non duplicate numbers as well.
johannesnestler 10-Jul-14 9:06am    
see my solution... (and accept if it fits your needs please)

1 solution

Hi ravikhoda,

Good Problem to use LINQ:

Here is a Little sample program

C#
using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] aNumbers = { 1, 1, 2, 2, 3, 4, 5, 6, 6, 7 };

            var indices = from number in aNumbers.Distinct()
                             select aNumbers.ToList().LastIndexOf(number);
            int[] aIndices = indices.ToArray();

            foreach (int iIndex in aIndices)
                Console.WriteLine(iIndex);

            Console.ReadKey();
        }
    }
}

The idea is to select only the distinct values from the original array. Then we call LastIndexOf for every distinct number on the original array. By this expression we build a IEnumerable of our indices. This is converted to your desired array of integers output-type (would not be needed just for enumerating like in the above sample).

Hope this helps

Kind regards Johannes
 
Share this answer
 
v2
Comments
Maciej Los 10-Jul-14 8:43am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900