Best algorithm for delete duplicates in array of strings

前端 未结 7 2025
一个人的身影
一个人的身影 2021-02-09 02:44

Today at school the teacher asked us to implement a duplicate-deletion algorithm. It\'s not that difficult, and everyone came up with the following solution (pseudocode):

<
7条回答
  •  一个人的身影
    2021-02-09 02:54

    This is the shortest algorithm that worked where arrNames and arrScores is parallel arrays and the highest score is taken.

    I := 0;
    J := 0;
    //iCount being the length of the array
    
    for I := 1 to iCount do
    for J := I + 1 to iCount do
    
       if arrNames[I] = arrNames[J] then
       begin
    
         if arrScores[I] <= arrScores[J] then
         arrScores[I] := arrScores[J];
    
       arrScores[J] := arrScores[iCount];
       arrNames[J] := arrNames[iCount];
    
       arrScores[iCount] := 0;
       arrNames[iCount] := '';
    
       Dec(iCount);
       end;
    

提交回复
热议问题