How to efficiently remove duplicates from an array without using Set

后端 未结 30 2789
情深已故
情深已故 2020-11-22 07:29

I was asked to write my own implementation to remove duplicated values in an array. Here is what I have created. But after tests with 1,000,000 elements it took very long ti

30条回答
  •  春和景丽
    2020-11-22 07:54

    int tempvar=0; //Variable for the final array without any duplicates
         int whilecount=0;    //variable for while loop
         while(whilecount<(nsprtable*2)-1) //nsprtable can be any number
         {
    //to check whether the next value is idential in case of sorted array       
    if(temparray[whilecount]!=temparray[whilecount+1])
            {
                finalarray[tempvar]=temparray[whilecount];
                tempvar++;
                whilecount=whilecount+1;
            }
            else if (temparray[whilecount]==temparray[whilecount+1])
            {
                finalarray[tempvar]=temparray[whilecount];
                tempvar++;
                whilecount=whilecount+2;
            }
         }
    

    Hope this helps or solves the purpose.

提交回复
热议问题