Best algorithm for delete duplicates in array of strings

前端 未结 7 1894
一个人的身影
一个人的身影 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 03:14

    If the order of the final solution is irrelevant, you could break the array into smaller arrays based on length of the strings, and then remove duplicates from those arrays. Example:

    // You have 
    {"a", "ab", "b", "ab", "a", "c", "cd", "cd"}, 
    
    // you break it into 
    {"a", "b", "a", "c"} and {"ab", "ab", "cd", "cd"}, 
    
    // remove duplicates from those arrays using the merge method that others have mentioned, 
    // and then combine the arrays back together into 
    {"a", "b", "c", "ab", "cd"}
    

提交回复
热议问题