Why duplicates appear
A domain name and its www version are technically different hostnames. Combined with the http and https choices, you have four possible addresses for every page. If the server answers on all of them without redirecting, links and visitors get split across versions, and search engines must guess which one to show.
Analytics can also split too, with some visits recorded under one address and some under another, which makes your numbers harder to trust.
Which version to choose
Functionally, either works. Many sites prefer the bare domain for shorter printed addresses. Others prefer www because it offers a bit more flexibility with DNS, as a CNAME can sit on www but not on the root. Whatever you pick, always use https.
Choose by looking at what you already have. If most of your backlinks, business listings and printed materials show one form, stay with it and redirect the other. Consistency matters more than the choice itself.
How to set up the redirect
Use 301 redirects from every non-preferred version to the preferred one, preserving the path. So http://www.example.com/services should go straight to https://example.com/services, in one hop if possible. Hosts and builders often have a setting for this, such as a primary domain option. On servers, a few lines of rules in the configuration do it.
Make sure DNS has records for both the root and www so the redirect has something to answer.
Make everything else agree
Your canonical tags, XML sitemap, internal links, structured data, social profiles and Google Business Profile link should all use the same preferred form. Mixed signals defeat the purpose. In Search Console, verify the domain-level property so you see data for all versions together.
Update printed materials and email signatures over time to match, though redirects will catch older ones.
How to verify it
Type each of the four versions of your home page into a browser and watch where you end up. All should land on the same final address. A redirect checker tool can display each hop and status code. Also test an inner page, not just the home page, since some rules only handle the front page.
Check again after any hosting change, SSL renewal or plugin install, since these can quietly alter the setup.
A common mix-up on new sites
Imagine a contractor whose old site lived at the www address, and whose new builder site defaults to the bare domain. For a few weeks, half the backlinks point to www, which now redirects. That is fine as long as the redirect is a permanent one and the old address still resolves. The problem arises when the www record was deleted from DNS during the move, leaving old links to fail with an error.
Keep both records alive in DNS for as long as you own the domain, even if only one address is the preferred one. It costs nothing and keeps every old link working.
Common questions
Does www or non-www matter for rankings?
No. Search engines treat either fine. What matters is picking one and being consistent.
Should I use a canonical tag instead of a redirect?
Use the redirect for hostname and protocol. Canonical tags are for page-level duplicates.
What if my site works on both without redirecting?
Add redirects. Otherwise, links and signals can be split between duplicate versions.
This guide is general information. Search engines change how they work and their guidelines; check Google's current documentation for your situation. Nobody can guarantee a ranking.