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

Related Questions

I have a comma separated string which might contain empty fields. For example: 1,2,,4
I have implemented a function TraverseStringClockwise which takes a comma separated string of integers,
I have a string named itemIDs separated by commas(12,43,34,..) and in order to use
I have comma-separated values. I need a query to insert into SQL Server 2008
I have a DB table that contains a comma separated list of ID's (ints)
I have text box in which your adds values in comma separated values. Once
How to split the comma separated string with out using loop fo example:- I
I have strings which contains thousand separators, however no string-to-number function wants to consume
Every once in a while, I need to build a character separated string while
I need to write a regex which matches strings representing comma separated days of

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.