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 521247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:13:49+00:00 2026-05-13T08:13:49+00:00

I have a comma separated string, out of which I need to create a

  • 0

I have a comma separated string, out of which I need to create a new string which contains a random order of the items in the original string, while making sure there are no recurrences.
For example:
Running 1,2,3,1,3 will give 2,3,1 and another time 3,1,2, and so on.

I have a code which picks a random item in the original string, and then iterates over the new string to see if it does not exist already. If it does not exist – the item is inserted.
However, I have a feeling this can be improved (in C# I would have used a hashtable, instead of iterating every time on the new array). One improvement can be removing the item we inserted from the original array, in order to prevent cases where the random number will give us the same result, for example.

I’d be happy if you could suggest improvements to the code below.

originalArray = originalList.split(',');            
for (var j = 0; j < originalArray.length; j++) {
    var iPlaceInOriginalArray = Math.round(Math.random() * (originalArray.length - 1));
    var bAlreadyExists = false;             
    for (var i = 0; i < newArray.length; i++) {
        if (newArray[i].toString() == originalArray[iPlaceInOriginalArray].toString()) {
            bAlreadyExists = true;
            break;
        }
    }

if (!bAlreadyExists)
    newArray.push(originalArray[iPlaceInOriginalArray]);
}

Thanks!

  • 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-13T08:13:50+00:00Added an answer on May 13, 2026 at 8:13 am

    You can still use a ‘hash’ in javascript to remove duplicates. Only in JS they’re called objects:

    function removeDuplicates(arr) {
        var hash = {};
        for (var i=0,l=arr.length;i<l;i++) {
            hash[arr[i]] = 1;
        }
        // now extract hash keys... ahem...
        // I mean object members:
        arr = [];
        for (var n in hash) {
            arr.push(n);
        }
        return arr;
    }
    

    Oh, and the select random from an array thing. If it’s ok to destroy the original array (which in your case it is) then use splice:

    function randInt (n) {return Math.floor(Math.random()*n)}
    
    function shuffle (arr) {
        var out = [];
        while (arr.length) {
            out.push(
                arr.splice(
                    randInt(arr.length),1 ));
        }
        return out;
    }
    
    // So:
    newArray = shuffle(
                   removeDuplicates(
                       string.split(',') ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Objective-C has the concept of a Protocol, which is the… May 13, 2026 at 11:13 pm
  • Editorial Team
    Editorial Team added an answer To load an image from a JAR resource use the… May 13, 2026 at 11:13 pm
  • Editorial Team
    Editorial Team added an answer The state of the problem at any point in time… May 13, 2026 at 11:13 pm

Related Questions

I know that this type of question has been asked over and over again,
The current situation is that topics are sorted by 3 main categories. There is
So i am working with some email header data, and for the to:, from:,
I'll begin by admitting that my problem is most likely the result of bad
Good day members, I have an input file which has rows of numerical digits

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.