Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:09:57+00:00 2026-05-27T08:09:57+00:00

This refers to one of my previous questions: array_unique vs array_flip – This states

  • 0

This refers to one of my previous questions: array_unique vs array_flip – This states that array_flip(array_flip()) is much quicker than array_unique() when dealing with simple strings and integers.

What I would like to know is why array_unique() creates a copy of the array, sorts it then removed the duplicates

The source for both functions is available here.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:09:58+00:00Added an answer on May 27, 2026 at 8:09 am

    If you think about it algorithmically, the way to remove duplicates is to go through a list, keep track of items you find, and get rid of things that are already in that “found this” list. One easy way to accomplish this is to sort a list. That way it’s obvious where to remove duplicates efficiently. Think about you, let alone a computer; which one of these lists is easier to remove duplicates from?

    apple
    banana
    cantaloupe
    apple
    durian
    apple
    banana
    cantaloupe
    

    or

    apple
    apple
    apple
    banana
    banana
    cantaloupe
    cantaloupe
    durian
    

    Edit: After looking into it a bit (and finding this article), it looks like while the two both get the job done, they are not functionally equivalent, or at least they aren’t always. To paraphrase a couple of these points:

    1. array_unique() sorts the values, as you noted, so array_flip(array_flip()) wouldn’t return the same-ordered array — but this might be desired.
    2. If the values are objects, then you can’t make them keys (right?), i.e. the flip method wouldn’t work out of the box on all arrays, whereas the sort method works fine, regardless of the value types.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone. I've written this simple function that is supposed to load a portion
This question refers to this one along with its accepted answer posted here on
Refer to my previous Questions: Get count of items and their values in one
A topic of many previous questions I see - but not quite as this
This question refers to this prior one posted here on SO by Christian Metzler
So I'm a little confused by this terminology. Everyone refers to Asynchronous computing as
This is a variation on a previous question as I am having tons of
Refer to this post for discussions on previous versions. Asking the question again since
The R 5 RS spec states that as part of the requirements for a
All- I know this has been asked, but the previous solutions don't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.